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:


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:


This works.

But when I activate SEF URL the URL changes to


and the extended URL would be


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?

1 Answer 1

  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 = '?';
        $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
    Commented Jan 28, 2015 at 18:12

Your Answer

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

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