5

I am using Joomla 3.3 and K2 v2.6.9.

I have a category view with several items, namely books. The URL looks like this:

http://localhost/index.php?option=com_k2&view=itemlist&task=category&id=214:publikationen&Itemid=203

To present these items in a CSV-style list, I need to use another template, called csv-view. So I create the link to this view by this code::

<?php echo $_SERVER['REQUEST_URI']; ?>&amp;template=csv-view

resulting in this URL:

http://localhost/index.php?option=com_k2&view=itemlist&task=category&id=214:publikationen&Itemid=203&template=csv-view

This works.

But when I activate SEF URL the URL changes to

http://localhost/navigation/forschung/itemlist/category/214-publikationen.html

and the extended URL would be

http://localhost/navigation/forschung/itemlist/category/214-publikationen.html&template=csv-view

and this page can't be found.

So here is my question: how can I construct the correct URL for the CSV view even if SEF URLs are activated?

4
  1. Instead of $_SERVER['REQUEST_URI'], you can rely on JUri::root()

  2. template parameter can not be added concatenating it with &. It has to be ? ... but it depends on the Url query. Thus, you need a piece of code like this to define the glue:

    $uri = JUri::getInstance($url);
    $query = $uri->getQuery();
    
    if (empty($query))
    {
        $glue = '?';
    }
    else
    {
        $glue = '&';
    }
    

As suggested by @fruppel in a more compact way:

    $uri = JUri::getInstance($url); 
    $uri->setVar('tmpl', 'csv-view'); 
    $url = $uri->toString();
  • You could use setVar which will automatically add a '?' or '&': $uri->setVar('tmpl', 'csv-view'); – fruppel Jan 28 '15 at 18:12

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.