Search code examples
joomlaurl-routingjoomla1.5joomla-sef-urlssef

How to use JRouter to rewrite controller and task URLs?


I'm using Joomla 1.5 with router.php file to rewrite URLs of views. Here are contents of router.php:

<?php
function PvcCalcProBuildRoute( &$query )
{
  $segments = array();
  if(isset($query['view']))
  {
    $segments[] = $query['view'];
    unset( $query['view'] );
  }
  return $segments;
}
function PvcCalcProParseRoute( $segments )
{
  $vars = array();
  switch($segments[0])
  {
    case 'cart':
    $vars['view'] = 'cart';
    break;
    case 'checkout':
    $vars['view'] = 'checkout';
    break;
    case 'login':
    $vars['view'] = 'login';
    break;
    case 'orders':
    $vars['view'] = 'orders';
    break;
    case 'offers':
    $vars['view'] = 'offers';
    break;
  }
  return $vars;
}
?>

I need to enable URL rewriting for controller and task. For example this line:

JRoute::_('index.php?option=com_pvccalcpro&amp;task=helpers');

I want to convert to /component/pvccalcpro/helpers.js

And this line:

JRoute::_('index.php?option=com_pvccalcpro&amp;controller=orders&amp;task=js');

I want to convert to /component/pvccalcpro/orders.js

I tried to figure out how to accomplish that with official Joomla Routing Manual, but after spending so many days it still didn't work out for me. Perhaps I'm missing something?

Here are contents of my orders controller for example:

<?php
// no direct access
defined('_JEXEC') or die();
jimport('joomla.application.component.model');
class PvcCalcProControllerOrders extends PvcCalcProController
{
    function display()
    {
      parent::display();
    }

    function js()
    {
      $t=time()+31536000;
      $expires = gmdate('D, d M Y H:i:s \G\M\T',$t);
      header('Content-type: text/javascript; charset=utf-8');
      header('Vary: Accept-Encoding');
      header('Last-Modified: Tue, 27 Dec 2001 07:05:43 GMT');
      header('Expires: '.$expires);
      header('Cache-Control: public, max-age=31536000');
      require_once(JPATH_COMPONENT.DS.'assets/js/orders.js');
      die();
    }
}
?>

Solution

  • In your two JRoute examples you're passing a path to your component but expecting it to return a path to your Javascript files.

    JRoute is for creating human readable URLs for Joomla! components you are trying to use it for Javascript files by the looks of things.

    To quote the Joomla! Doc's page - "Joomla can, initially, only create human readable URLs for built-in components."