1

I have created a custom component with routing that seems to be mostly correct and have now created a module to display multiple items of the component on the homepage.

I have got it to a point where the url is https://www.site.com/component/menu-item/child-menu-item/item-name

If I show a list of items from a menu item then the URL is https://www.site.com/menu-item/child-menu-item/item-name so this is correct.

The thing I am trying to do is remove the /component/ portion of the URL however I can't find where to do this. I have printed out the query variable in my component router file but it doesn't have this segment, its like it is being added before I add my segments in my router.php file.

Is there an easy way to remove the /component/ portion of the url? Please note that the item name will be dynamic as the component is always growing with new pages so I can't have a static re-write of the URL.

Thanks

2 Answers 2

5

The /component/name/ part of the url is automatically written by Joomla router when there is no matching menu item found. This is required for the application to route the command to the proper component.

So in your case, the pattern can be removed by creating the menu item. Since the menu item is already mapped to a component view, the routing will happen correctly.

2
  • Hi @Nagarjun thanks for your answer it makes some sense however how do I go about doing this? Seeing as my module is displaying items from the component, do I need to make a menu item that shows a single item from the component for it to work? I'm unsure how I can 'link' the module with a menu item.
    – Alex Mehan
    Sep 5, 2017 at 0:23
  • If you think about the Joomla articles structure, every article has a category. So creating the menu item for the top level category would suffice for all articles. If your component has a similar structure, it is what I suggest. In your router file make changes to get the category menu in case no single item menu exists.
    – Nagarjun
    Sep 5, 2017 at 9:52
2

There is a very simple and handy way to do it, you can create a menu at the time of install in script file in method public function postflight($type, $parent).

Use a process here as follows to create a menu type and then a menu item =>

https://webkul.com/blog/menu-item-joomla-rebuild-automatically/

This will create a menu item of your view.

Get data before using in the module

$db = JFactory::getDbo();

// Create a new query object.
$query = $db->getQuery(true);

$query->select($db->quoteName(array('id', 'title')));
$query->from($db->quoteName('#__menu'));
$query->where($db->quoteName('link') . "='index.php?option=com_custom&view=custom'");
$query->where($db->quoteName('menutype') . "='custom'");

// Reset the query using our newly populated query object.
$db->setQuery($query);

// Load the results as a list of stdClass objects
$results = $db->loadObjectList();

Now create a seo link as below

JRoute::_("index.php?option=com_custom&view=custom&Itemid=".$results[0]->id,false)

For using at multiple places you can keep this in a helper function to retrieve menu item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.