http-redirectjoomlaroutesjoomla-sef-urls

Using component's routes from 'router.php' in custom Plugin in Joomla


Question: How do I enable the Routes from my component's router.php in a plugin?

I'm working on a custom Plugin which redirects the route from default user profile:

index.php?option=com_users&view=profile (SEF: /component/users/profile)

to my own component where I've got additional settings

index.php?option=com_mycomponent&view=profile (SEF: /alias/profile)

my front-end plugin:

class plgSystemMyPlugin extends JPlugin
{
   // constructor
   function plgSystemMyPlugin( &$subject, $params ) {
        parent::__construct( $subject, $params );
   }

   // run after the framework has loaded and the application initialize method has been called
   function onAfterInitialise() {

        // when component users and view profile are called
        if( isset($_GET['option'], $_GET['view'])
            && $_GET['option'] == 'com_users'
            && $_GET['view'] == 'profile' )
        {
                $route = JRoute::_('index.php?option=com_mycomponent&view=profile' );
                JFactory::getApplication()->redirect($route, null, null, true);
        }
    }
}

In my component all links are routed correctly ie:

index.php?option=com_mycomponent&view=profile => /alias/profile

in the plugin JRoute translates it as follows:

index.php?option=com_mycomponent&view=profile => /component/mycomponent/profile

can not use:


Solution

  • in the plugin xml file you should add new parameter that will allow you to choose the desired Itemid(menuitem) so it should look like this

    <param name="menuitem" name="my_itemid" title="Target Itemid" description=""/>
    

    Then you will need to choose desired menuitem that has the alias that you wanted from the plugin parameters in the administrator area then in the plugin itself just use like this:

    $route = JRoute::_('index.php?Itemid='.$this->params->get('my_itemid') );
    

    and this is valid too

    $route = JRoute::_('index.php?option=com_mycomponent&view=profile&Itemid='.$this->params->get('my_itemid') );