Search code examples
cakephpurl-routingcakephp-1.3

CakePHP reverse routing not working as expected


I need several routes in my application to allow for a dynamic string to proceed the prefix.

Here's my route:

Router::connect('/:location/traveler/:controller/*', array('action' => 'index', 'traveler' => true, 'prefix' => 'traveler'), array('pass' => array('location')));

For instance, if I went to /south/traveler/requests it would route successfully to RequestsController::traveler_index($location = 'south').

This is what I want, but I also need HtmlHelper::link() to properly reverse route a URL array into that route.

Here's my call to HtmlHelper::link():

$this->Html->link('List Requests', array('controller' => 'requests', 'action' => 'index', 'location' => 'south'));

The prefix routing is (or should be) implied since this is being called from a view within the traveler prefix.

The URL that call spits out is:

http://domain.com/traveler/requests/location:south

Have I not done something correctly? Is there any way I can avoid creating a custom route class to properly reverse route these URL arrays?


Solution

  • I solved the problem.

    Removing Router::connectNamed() from routes.php, I repaired my route which was misconfigured.

    The reverse route to traveler_index() worked properly using the route I listed above, but any call to any other function, like traveler_edit() would fail.

    Using the route below, I was able to get it to reverse route for any action on any controller in the traveler prefix with location as a variable.

    Router::connect('/:location/traveler/:controller/:action/*', array('traveler' => true, 'prefix' => 'traveler'), array('pass' => array('location')));
    

    Now, my call to HtmlHelper::link() correctly reverse-routes my URL array:

    $this->Html->link('Edit Details', array('controller' => 'requests', 'action' => 'edit', 'location' => 'south', 1234));
    

    ...reverse routes to /south/traveler/requests/edit/1234.