简体   繁体   English

强制Joomla JRoute使用菜单项

[英]Force Joomla JRoute to use the menu item

I'm building a component for Joomla! 我正在为Joomla构建一个组件! 2.5 and inside I'm using JRoute::_('index.php?option=com_myapp&view=cpanel') to build all my links. 2.5和内部我使用JRoute::_('index.php?option=com_myapp&view=cpanel')来构建我的所有链接。 This works, BUT it produces links which look like that: 这有效,但它产生的链接看起来像这样:

/component/myapp/cpanel.html

In the menu however I have defined that index.php?option=com_myapp&view=cpanel has an alias of "myapp" so the link should rather be 但是在菜单中我已经定义了index.php?option=com_myapp&view=cpanel的别名为“myapp”,所以链接应该是

/myapp/cpanel.html

The component is accessible via this path. 可以通过此路径访问该组件。 If I do that, also all links generated inside will have the /myapp prefix. 如果我这样做,内部生成的所有链接都将具有/myapp前缀。 But for use in a template (special login link) and if the user stumbles upon /component/myapp .... I still want all links to go to /myapp prefix. 但是要在模板(特殊登录链接)中使用,如果用户偶然发现/component/myapp ....我仍然希望所有链接都转到/myapp前缀。

How can I force JRoute to use this menu item entry by itself? 如何强制JRoute单独使用此菜单项?

//look if there is a menu item set for myapp. if yes, we use that one to redirect
$db  = JFactory::getDBO();
$defaultRedirect = 'index.php?option=com_myapp&view=cpanel';
$db->setQuery('SELECT `id` FROM #__menu WHERE `link` LIKE '. $db->Quote($defaultRedirect) .' LIMIT 1' );
$itemId = ($db->getErrorNum())? 0 : intval($db->loadResult());
if($itemId){
    $rpath = JRequest::getString('return', base64_encode(JRoute::_('index.php?Itemid='.$itemId)));
}else{
    $rpath = JRequest::getString('return', base64_encode(JRoute::_('index.php?option=com_myapp&view=cpanel')));
}

Beware: This is NOT language-safe. 注意:这不是语言安全的。 It takes the first menu entry found in the db. 它需要在db中找到第一个菜单项。 If you setup different menu aliases for different languages you have to check for the right language in the SQL query, too. 如果为不同的语言设置不同的菜单别名,则还必须在SQL查询中检查正确的语言。

Because I struggled a lot with this issue, here's a quick function I did that will try to find the link based on a menu item. 因为我在这个问题上遇到了很多困难,所以这是一个快速的功能,我会尝试根据菜单项找到链接。 It's mainly based on other answers. 它主要基于其他答案。

This has only been tested on Joomla 3.5. 这只是在Joomla 3.5上测试过的。 JRequest is deprecated, so $app->input is used instead. 不推荐使用JRequest,因此使用$ app-> input。 This might be done better, but at least it works for the use I have of it. 这可能会做得更好,但至少它适用于我的用途。 Hopes it can help other people too ! 希望它也可以帮助其他人!

public static function getRouteMenu($default_url) {
    $db = JFactory::getDbo();
    $app = JFactory::getApplication();
    $jinput = $app->input;

    $db->setQuery('SELECT `id` FROM #__menu WHERE `link` LIKE '. $db->Quote($default_url) .' AND `published` = 1 LIMIT 1' );
    $itemId = $db->loadResult();
    if($itemId) {
        $route = $jinput->getString('return', JRoute::_('index.php?Itemid='.$itemId));
        $route = str_replace('index.php/', '', $route);
    } else {
        $route = $jinput->getString('return', JRoute::_($default_url));
    }

    return $route;
}

The parameter requires a not rewrited link such as : index.php?option=com_content&view=article&id=10 . 该参数需要一个未重写的链接,例如: index.php?option=com_content&view=article&id=10 Check your database if you need to be sure about how to build up this link. 如果您需要确定如何构建此链接,请检查您的数据库。

You ideally need to set a router as part of your component. 理想情况下,您需要将路由器设置为组件的一部分。 The menu manager will override the component/myapp bit, but what comes after that will be determined by what is in the router.php file. 菜单管理器将覆盖组件/ myapp位,但之后的内容将由router.php文件中的内容决定。

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

If, however, you simply need to create a link to a specific menu item, then you can always just add an Itemid to your parameter 但是,如果您只需要创建指向特定菜单项的链接,那么您始终只需将Itemid添加到参数中即可

JRoute::_('index.php?Itemid=12')

will point to any menu item with the id 12, so if that is your menu item ID for your com_myapp component, it will create the menu version of the link to that page. 将指向任何ID为12的菜单项,因此如果这是com_myapp组件的菜单项ID,它将创建指向该页面的链接的菜单版本。

edit: 编辑:

You could technically assign the same component to two menu items, so I don't believe that handling the beginning of the URL is default behaviour when you link to a component outside of a menu item without giving the link an itemid. 您可以在技术上将相同的组件分配给两个菜单项,因此当您链接到菜单项外部的组件而不给链接指定itemid时,我不认为处理URL的开头是默认行为。 Joomla say (on creating a link for JRoute)... Joomla说(在创建JRoute的链接时)......

"Notice that it is possible to leave out the parameters option and Itemid. option defaults to the name of the component currently being executed, and Itemid defaults to the current menu item's ID." “请注意,可以省略参数选项和Itemid。选项默认为当前正在执行的组件的名称,而Itemid默认为当前菜单项的ID。”

ie, call it from your homepage (or any page outside of one set to use your component) without an itemid or option and it is likely to think you are in com_content and with the homepage menu item. 即,在没有itemid或选项的情况下,从您的主页(或一组以外的任何页面)调用它而不使用itemid或选项,它可能会认为您处于com_content和主页菜单项。

If you want to link to the component outside of a menu item but without hard-coding an itemid, then one way would be to add a Menuitem field type to the config options of the module or template where you are linking from, and then add the itemid it generates to your link. 如果要链接到菜单项之外的组件但没有对itemid进行硬编码,那么一种方法是将Menuitem字段类型添加到要链接的模块或模板的配置选项中,然后添加它为您的链接生成的itemid。

You could also use JFactory::getApplication()->getMenu() to find which menu item is linking to your component. 您还可以使用JFactory :: getApplication() - > getMenu()来查找链接到组件的菜单项。 Not had chance to test this - but using that could also mean that you can set an itemid parameter in your router. 没有机会对此进行测试 - 但使用它也可能意味着您可以在路由器中设置itemid参数。

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

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