简体   繁体   English

Joomla从后端的模型获取SEF前端URL

[英]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. 我在Joomla中制作了一个自定义组件,需要向用户发送邮件。 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. 然后,我将以上链接用作电子邮件正文上按钮的锚点,并通过JMAIL发送。 However the link that arrives is a non-sef one (as SEF is not applicable on backend). 但是,到达的链接是非安全链接(因为SEF在后端不适用)。 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): 而我想要的结果是(出现一个名为mail的菜单):

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. 有谁知道如何从后端代码中获取SEF前端URL。 Thanks in advance. 提前致谢。

I know it's quite outdated but it's the first (relevant) result on Google. 我知道它已经过时了,但这是Google上的第一个(相关)结果。 So you need the site (frontend) router and an ItemID. 因此,您需要站点(前端)路由器和一个ItemID。 That's all. 就这样。 J!2.5 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. 第一个方法是使链接端点指向前端MVC路径,在该路径中,模型类在后端扩展自定义消息模型。

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. 这使我们指向第2点。您可以通过mod_menus模块创建一个不在网站上显示的菜单类型,然后将菜单链接添加到前端视图,并使用别名保存您想要的别名。 This provides Joomla a reference point. 这为Joomla提供了参考点。 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. “正确”的方法是编写一个router.php脚本并将其放置在组件的根站点文件夹中。

If you write the router.php script, this will enable the automated formatting of URLs to SEF format when using JRoute::_() with one caveat. 如果编写router.php脚本,则在将JRoute :: __()用作警告时,这将启用将URL自动格式化为SEF格式的功能。 If you don't, you need to manually format the link URL to point to: www.mydomain.com/my-menu-alias 如果不这样做,则需要手动设置链接URL的格式,使其指向:www.mydomain.com/my-menu-alias

I attached links for the router.php script, which are now relevant by following step 1. 我附加了router.php脚本的链接,这些链接现在可以通过以下步骤1进行操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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