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

时间:2014-10-10 12:38:56

标签: php joomla

我在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。 提前谢谢。

2 个答案:

答案 0 :(得分:2)

我知道它已经过时了,但这是谷歌的第一个(相关)结果。 所以你需要站点(前端)路由器和ItemID。就这样。 J 1 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));

答案 1 :(得分:0)

实现你想要的东西需要两件大事。第一种方法是将链接端点设置为前端MVC路径,其中模型类在后端扩展自定义消息模型。

这将我们带到第2点。你可以通过mod_menus模块创建一个没有在网站上显示的menutype,并添加一个菜单链接到前端视图,别名包含你想要的别名是什么。这为Joomla提供了一个参考点。这是快速而肮脏的方法。 "适当"方法是编写router.php脚本并放在组件根站点文件夹中。

如果您编写router.php脚本,这将使用JRoute :: _()时可以自动将URL格式化为SEF格式。如果您不这样做,则需要手动设置链接网址格式以指向:www.mydomain.com/my-menu-alias

我附上了router.php脚本的链接,现在通过以下步骤1进行了相关。

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