0
votes

Simple question for Cakephp 2.0.

I want to set a routing rule such that:

www.abc.com/z/abc123

will resolve to the full URL of (including the URL parameter)

www.abc.com/bookings/bookingref/?ref=abc123

Where bookings is the Controller, and bookingref is the action.

Can someone teach me what I need to write in the routes.php?

Kevin

3

3 Answers

0
votes

In routes.php:

Router::connect('/bookingref/', array('controller' => 'bookings', 'action' => 'bookingref'));

In controller:

public function bookingref(){


}

So you should have a view name after your function. i.e. bookingref.ctp

0
votes

This is how I would implement your solution:

In Config/routes.php add:

Router::connect('/z/:reference',
    ['controller' => 'bookings', 'action' => 'bookingref'],
    [
        'pass' => ['reference'],// Passed to corresponding function argument (order matters if 2 or more)
        'reference' => '[a-z0-9]+'// RegExp validation if you need it
    ]
 );

In your BookingsController use:

public function bookingref($reference = null)
{
...
}
0
votes

Unfortunately, Router::redirect() cannot redirect to string based URLs that include variables. The controller based approach Progredi mentioned is your best bet.