TransWikia.com

Joomla custom component - router not find alias

Joomla Asked by Lecraver on January 21, 2021

I am creating my own component.
Is it possible to transform:

Route::_('index.php?option=com_toto&view=tutu') into /somealiasinmenu?

(This alias can be found in a menu in the backoffice.)

I can do this:

  • link 1 /listAlias
  • link 2 /formAlias

When I click on listAlias, I can edit one row. With my router I have this url:

/listAlias/formviewname/123
but I’d like this url /formAlias/123

How can I do that?

Here is my router.php:

function TotoBuildRoute(&$query)
{
    $segments = array();

    if(isset($query['view'])){
        $segments[] = $query['view'];
        unset($query['view']);
    };

    if(isset($query['id'])){
        $segments[] = $query['id'];
        unset($query['id']);
    };

    return $segments;
}

function TotoPlaceParseRoute($segments)
{
    $vars = array();

    if ($segments[0] === 'formviewname') {
        $vars['id'] = (int)strstr(end($segments), ':', true);
    }
    $vars['view'] = $segments[0];

    return $vars;
}

One Answer

If I'm understanding you correctly, you have 2 menuitems on your site which have links /formAlias and /listAlias, and when you form an SEF URL using Route:_() then it gives you the wrong link; is that correct?

The way I believe Joomla routing works is that when you supply the parameters to Route::_() it goes through the site menuitems to compare the link attribute of each menuitem with the parameters you specify in your Route::_() call.

If it finds an exact match then it bases the SEF URL on the alias of that menuitem.

If it doesn't find an exact match, then the choice which Joomla makes will appear more random; I think the menuitem which the user is currently on will have a bearing.

Regardless of that, you can specify which menuitem Joomla should use in one of two ways (that I know of):

  1. By specifying the Itemid=xxx parameter in your Route::_() call (Itemid is the id of the menuitem you want to use)
  2. By changing the Itemid selected by Joomla in your custom router preprocess() function.

If you want to use the second option then there is an example in https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Adding_Associations in the Custom Router section, where the code checks if the menuitem associated with the Itemid which Joomla routing has provided has the right language, and if not it tries to find a better menuitem, with the correct language.

You would need to do a similar thing to decide whether your formAlias or listAlias menuitem is more appropriate in each particular case. There is a guide to using the Joomla menuitem API at https://docs.joomla.org/Menu_and_Menuitems_API_Guide

Answered by Robbie Jackson on January 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP