3

I am writing a component with two views - a list view, and an article view.

The list view generates the links for each article as follows, with $text being the variable I want in the URL.

<?php echo JRoute::_("index.php?Itemid=103&testvar=".strtolower($text)); ?>

ie http://www.test.com/test is the list page and I want http://www.test.com/test/text as the article page

103 is the menu item id where the component is. This is set to the list view.

My router contains the following code:

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

    };

This generates the following URL: http://www.test.com/test/text?testvar=text

unset($query['testvar']); does not seem to be working to remove the ?testvar=text bit. Everything else is working as expected.

A workaround would be <?php echo JRoute::_("index.php?Itemid=103")."/".strtolower($text); ?> - but that seems hacky.

Any ideas?

0

Take a look at the JUri::delVar method: https://api.joomla.org/cms-3/classes/Joomla.CMS.Uri.Uri.html#method_delVar

JUri::delVar('testvar');

Worked perfectly for me :)

  • 1
    I don't see this as a "link-only answer" It contained the one, single method call that is needed to do what OP wanted to do, JUri::delVar. The link to the docs was for additional information :) – Arnor Baldvinsson Oct 20 '18 at 18:02
  • Sorry if I'm stepping on somebody's toes, not intended. Been doing active developer support for 25+ years and sometimes there is only so much that can be said. – Arnor Baldvinsson Oct 21 '18 at 23:38
1

Assuming you can only have one list view, then your BuildRoute function should have a maximum of 1 segment (which is the article alias). You should unset all other segments (try doing a print_r to see what you should unset.

In your ParseRoute function, you check for the count of segments, and you return either the list view or the article view. Here's how:

if (count($segments) == '0'){ //it's a list view
    `return array('option'=>'com_yourcomponent', 'view'=>'list', 'Itemid'=>[the item id of your choice]);`
else //it's an article view
    `return array('option'=>'com_yourcomponent', 'view'=>'article', 'testvar' => $segments[0], 'Itemid'=>[the item id of your choice]);`

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.