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


Joomla obtain SEF Frontend URL from a model in backend

我正在Joomla中制作一个自定义组件,该组件需要向用户发送邮件。我使用该模型从后端管理部分发送邮件。该模型使用以下代码:

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

然后我使用上面的链接作为电子邮件主体上按钮的锚,并通过JMAIL发送。然而,到达的链接是非sef链接(因为sef不适用于后端)。这是我得到的链接:

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

而我想要的结果是(菜单以邮件的形式出现):

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

有人知道如何从后端代码中获取SEF前端URL吗。提前谢谢。

我知道它已经过时了,但这是谷歌上的第一个(相关的)结果。因此,您需要站点(前端)路由器和ItemID。仅此而已。J2.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));

要实现您想要的目标,需要两件大事。第一种是让链接端点指向前端MVC路径,在该路径中,模型类在后端扩展自定义消息模型。

这就引出了第二点。您可以通过mod_menus模块创建一个不显示在网站上的菜单类型,并在前端视图中添加一个菜单链接,其中包含您希望的别名。这为Joomla提供了一个参考点。这是一种快速而肮脏的方法。"正确"的方法是编写一个router.php脚本,并将其放在组件根站点文件夹中。

如果您编写router.php脚本,在使用JRoute::_()时,这将使URL自动格式化为SEF格式,但需要注意一点。如果没有,您需要手动格式化链接URL以指向:www.mydomain.com/my-menu-alias

我附加了router.php脚本的链接,通过下面的步骤1,这些链接现在是相关的。

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