I have a problem with routing in CakePHP. I want to pass parameter to route, but this parameter need to have default value. My route:
$routes->connect('/exchangeOffer/add/:type', ['controller' => 'Offer', 'action' => 'add'], ['pass' => ['type'], 'type' => '(NORMAL|AUTO)']);
This route works onnly when I go to one of addresses:
But I want it to work with address
And then pass default type parameter as NORMAL.
I know that something like this is posible in Zend Framework, but I'm totally new in CakePHP, and can't find a way to do this(looked through many post and answers, not only here, but none of them helped).
If you want to solve this at routing level, then you'll have to add an additional route with no :type
route element set, and a default type
param passed in the defaults array, like
$routes->connect(
'/exchangeOffer/add/:type',
[
'controller' => 'Offer',
'action' => 'add'
],
[
'pass' => ['type'],
'type' => '(NORMAL|AUTO)'
]
);
$routes->connect(
'/exchangeOffer/add',
[
'controller' => 'Offer',
'action' => 'add',
'type' => 'NORMAL'
],
[
'pass' => ['type']
]
);
And if you want to be able to generate URLs (for example via Router::url()
) without defining a type, you'll have to add (append - as order matters) a third rule with no type being involved at all
$routes->connect(
'/exchangeOffer/add',
[
'controller' => 'Offer',
'action' => 'add'
]
);
See also
If you don't need to the action,you just input Controller, cakephp will select the default action in controller ( it will be index ) example:
Router::connect(
'/add/*', array('controller' => 'users','action' => 'index' )
);
when ever you go go http://domain.com/add/some-thing.html or http://domain.com/add , system will call the controller USERS and action INDEX
Hope it help you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.