Il manuale»Sviluppare con CakePHP»Configurazione»Configurazione del sistema di Routing»Passing parameters to action
3.4.5.4 Passing parameters to action
Assuming your action was defined like this and you want to access the arguments using $articleID instead of $this->params['id'], just add an extra array in the 3rd parameter of Router::connect().
// some_controller.php
function view($articleID = null, $slug = null) {
// some code here...
}
// routes.php
Router::connect(
// E.g. /blog/3-CakePHP_Rocks
'/blog/:id-:slug',
array('controller' => 'blog', 'action' => 'view'),
array(
// order matters since this will simply map ":id" to $articleID in your action
'pass' => array('id', 'slug'),
'id' => '[0-9]+'
)
)
// some_controller.phpfunction view($articleID = null, $slug = null) {// some code here...}// routes.phpRouter::connect(// E.g. /blog/3-CakePHP_Rocks'/blog/:id-:slug',array('controller' => 'blog', 'action' => 'view'),array(// order matters since this will simply map ":id" to $articleID in your action'pass' => array('id', 'slug'),'id' => '[0-9]+'))
And now, thanks to the reverse routing capabilities, you can pass in the url array like below and Cake will know how to form the URL as defined in the routes.
// view.ctp
// this will return a link to /blog/3-CakePHP_Rocks
<?= $html->link('CakePHP Rocks', array(
'controller' => 'blog',
'action' => 'view',
'id' => 3,
'slug' => Inflector::slug('CakePHP Rocks')
)) ?>
// view.ctp// this will return a link to /blog/3-CakePHP_Rocks<?= $html->link('CakePHP Rocks', array('controller' => 'blog','action' => 'view','id' => 3,'slug' => Inflector::slug('CakePHP Rocks'))) ?>
See comments for this section
