3
votes

I am using CakePHP 1.3 and have some troubles with prefix routing.

I configured routes like that:

   Router::connect(
       '/listing/*',
       array(
           'controller' => 'dsc_dates',
           'action' => 'listing',
        )
   );

   Router::connect(
       '/modular/listing/*',
       array(
           'controller' => 'dsc_dates',
           'action' => 'listing',
           'prefix' => 'modular'
       )
   );

in my controller there are two functions:

function modular_listing($order = null,$orderDirection = null, $items=null, $location_id=null) {
   $this->layout='module';

   $this->setAction('listing',$order, $orderDirection, $items, $location_id);
}

function listing($order = null,$orderDirection = null, $items=null, $location_id=null){...}

The prefix action should just change some things and then operate like the normal 'listing' method. Until here it works fine.

But if i create relative links (with HTML Helper) Router::url() uses 'modular_listing' as action which does not fit into my routes. It should be 'listing' instead of 'modular_listing'. The controller params are correct with 'listing' as action but the router params still says 'modular_listing'.

So relative links:

$this->Html->link('example',array('parameter'));

will end up in:

/dsc_dates/modular_listing/parameter

How can I get the correct links so that the router uses 'listing' as action?

UPDATE: It is not an alternative to add 'controller' and 'action' to the url array of the link generation. In fact I have problems with the automatically generated relative links from the paginator.

1

1 Answers

2
votes

I couldn't tell if you wanted the generated Html->link() routes with the leading controller or not, so I did both:

Controller (note the renderer):

// DscDatesController.php

public function listing($param = null) {
    $this->set('param', $param);
    $this->render('listing');
}

public function modular_listing($param = null) {
    // 
    $this->setAction('listing', $param);
} 

Routes:

// routes.php

Router::connect(
   // notice no leading DS
   'listing/*',
    array(
       'controller' => 'DscDates',
       'action' => 'listing'
     )
);

Router::connect(
   '/modular/listing/*',
   array(
       'controller' => 'DscDates',
       'action' => 'listing'
   )
);

View:

// DscDates/listing.ctp

<?php

  // generates /dsc_dates/listing/:param
  echo $this->Html->link(
    'example',
    array('controller'=>'dsc_dates', 'action'=>'listing', $param));

  // generates /listing/:param
  echo $this->Html->link(
    'example',
    array('action'=>'listing', $param));

About wildcards, DS and routing order: CakePHP broken index method

HTH :)