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]+'
    )
)
  1. // some_controller.php
  2. function view($articleID = null, $slug = null) {
  3. // some code here...
  4. }
  5. // routes.php
  6. Router::connect(
  7. // E.g. /blog/3-CakePHP_Rocks
  8. '/blog/:id-:slug',
  9. array('controller' => 'blog', 'action' => 'view'),
  10. array(
  11. // order matters since this will simply map ":id" to $articleID in your action
  12. 'pass' => array('id', 'slug'),
  13. 'id' => '[0-9]+'
  14. )
  15. )

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')
)) ?>
  1. // view.ctp
  2. // this will return a link to /blog/3-CakePHP_Rocks
  3. <?= $html->link('CakePHP Rocks', array(
  4. 'controller' => 'blog',
  5. 'action' => 'view',
  6. 'id' => 3,
  7. 'slug' => Inflector::slug('CakePHP Rocks')
  8. )) ?>