0

I developed an ecommerce component and I have the following problem that I hope you could help me.

The component has a locations manager where we can enlist cities such as: Madrid, London, Paris, Rome.

Then when the user selects a city the website will filter products and companies by that city.

If I choose Madrid I will see products available for Madrid only.

Normally we publish a module above that allows the user to change city if he/she wants by showing a list of cities.

The link of a city will call a task in my controller that sets a cookie with the city ID and redirects to home page.

Then the content is filtered on the web using the value of the cookie.

Sorry for the tedious introduction.

My question:

The links are like:

http://theweb.com/deal/dealSetLocationCookie?locationId=1 http://theweb.com/deal/dealSetLocationCookie?locationId=2

or

http://theweb.com/index.php?option=com_mycom&controller=deal&task=dealSetLocationCookie&locationId=2

and my customers want:

http://theweb.com/madrid

http://theweb.com/london

I guess I could create menu elements using the links, but that means lot more work because some costumers have lots of cities and it is necessary to have then registered in my component and also in the menu manager too.

Is there a way for my module to generate links like

http://theweb.com/madrid

but internally meaning http://theweb.com/deal/dealSetLocationCookie?locationId=1 ?

Or should I try to automatic generate menu elements from my locations manager, but how?

Any other idea.

Thanks for your help

2

Ofcourse you can use clean urls in your component. For this, you will need to modify the router.php file in your component for site.

Please refer the following link for it: https://docs.joomla.org/Supporting_SEF_URLs_in_your_component

Also you can refer the following example:

Function: ComponentNameBuildRoute in router.php :

function ComponentNameBuildRoute(&$query)
{
if(isset($query['view'])){
        $segments[] = $query['view'];
        unset($query['view']);
    }
if(isset($query['locationId'])){
        $segments[] = $query['locationId'];
        unset($query['locationId']);
    }
if(isset($query['task'])){
        $segments[] = $query['task'];
        unset($query['task']);
    }
}//function ends : here you add those paramaters passed by you in the link

Next function: ComponentNameParseRoute in router.php : Segment according to your indexing. This is just an example:

function ComponentNameParseRoute($segments){
    $vars = array();
    switch($segments[0])
           {
               case 'viewname1':
                       $vars['view'] = 'viewname1';
                       $vars['task'] = $segments[1];
                       $vars['locationId'] = $segments[2];
                       break;
                case 'viewname2':
                       $vars['view'] = 'viewname2';
                       $vars['locationId'] = $segments[0];
                       break;
    }
}

Hope this helps

  • Hi, thanks for your answer. Using ComponentNameBuildRoute I was able to create links like theweb.com/madrid and I will now try to put the url back again with ComponentNameParseRoute . However, remember I and using a module and it works well when the main component in the page is the same but if I go to, lets say, to a joomla article, jroute gives me theweb.com/component/mycomponent/madrid – Piero Marsilio Jan 7 '16 at 15:25
  • @PieroMarsilio I am happy that my answer helped you. You can also accept the answer if it worked for you :) And for removing component/mycomponent/ did you enabled the url rewriting option from global config? – user5491 Jan 7 '16 at 16:28
  • Hi, thanks again for your help. I am using url rewriting option. When the page being shown belong to my component, JRoute works ok and the links in the module are like theweb.com/madrid , but when we are in another component page, like com_content, JRoute makes the link to be like theweb.com/component/mycomponent/madrid . It is like your route.php can be only executed if the page is its component. – Piero Marsilio Jan 8 '16 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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