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?

2 Answers 2

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 :)

2
  • 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 :) Oct 20, 2018 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. Oct 21, 2018 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 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.