Sebastian
Sebastian

Reputation: 923

CakePHP 2.x Custom Route with Arguments

In my CakePHP App I'd like to pass arguments in a custom route.

What works now

(domain/controller/action/param)

domain.com/dealers/view/1

What I'd like to do

(domain/param/controller/action/param)

domain.com/washington/dealers/view/1

This is my code in routes.php:

Router::connect('/:city/dealers/view/:id', array('controller' => 'dealers', 'action' => 'view'), 

    array(
        'pass' => array('city', 'id')
        ),

    array('city' => '[a-z]+')
    );

This just redirects domain.com/washington/dealers/view/1 to domain.com/dealers/index for the obvious reason that I did not properly pass the parameters. Does anyone know what I am missing?

Upvotes: 1

Views: 2750

Answers (1)

amstegraf
amstegraf

Reputation: 597

city should not be in a separate array ex:

Router::connect(
   '/:city/dealers/view/:id', 
   array('controller' => 'dealers', 'action' => 'index'),
   array(
    'pass' => array('city', 'id'),
    'city' => '[a-z]+'
));

Upvotes: 3

Related Questions