简体   繁体   中英

Joomla obtain SEF Frontend URL from a model in backend

I am making a custom component in Joomla which needs to send a mail to user. I use the model to send the mail from the backend admin section. The model uses this code :

$btn_link = JURI::root().substr(JRoute::_("index.php?option=com_mymailer&view=messages&email={$myMsg->email}&tid={$myMsg->ticket_id}");

Then I use the above link as an anchor on a button on email body and send through JMAIL. However the link that arrives is a non-sef one (as SEF is not applicable on backend). This is the link I get :

http://testsite.com/index.php?option=com_mymailer&view=messages&email=mytestmail@gmail.com&tid=27

Whereas the result I want is (a menu is present named as mails):

http://testsite.com/mails/messages/index.php?email=mytestmail@gmail.com&tid=27

Do any body have an idea how to get SEF frontend URLs from the backend code. Thanks in advance.

I know it's quite outdated but it's the first (relevant) result on Google. So you need the site (frontend) router and an ItemID. That's all. J!2.5

//get the frontend Application to create the full object chain
$siteApp = JApplication::getInstance('site');

//now you can use the router for the frontend
$siteRouter = $siteApp->getRouter();

//get the itemid as you wish
//you can use frontend menu object
$siteMenu = $siteApp->getMenu();

//you need a correct itemid in $yourUri to route correctly
/* there's a little glitch, maybe with JURI::root so it still uses 
the admin subdir anyways, but simply remove it. 
maybe better to check if it's at the beginning of the string 
but now it's your business */

$sefURI = str_replace('/administrator',NULL,$siteRouter->build($yourUri));

To achieve what you want requires two major things. The first is to have the link endpoint be to a front end MVC path where the model class extends the custom messages model in the back end.

Which brings us to point 2. You can either create a menutype which is not displayed on the website via the mod_menus module, and add a menu link to the front end view with the alias holding what you would like the alias to be. This provides Joomla a reference point. That's the quick and dirty method. The "proper" method would be to write a router.php script and place in your components root site folder.

If you write the router.php script, this will enable the automated formatting of URLs to SEF format when using JRoute::_() with one caveat. If you don't, you need to manually format the link URL to point to: www.mydomain.com/my-menu-alias

I attached links for the router.php script, which are now relevant by following step 1.

http://docs.joomla.org/Supporting_SEF_URLs_in_your_component http://docs.joomla.org/Search_Engine_Friendly_URLs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM