Question

I have one route that looks like this:

Router::connect('/Album/:slug/:id',array('controller' => 'albums', 'action' =>    'photo'),array('pass' => array('slug','id'),'id' => '[0-9]+'));

and another like this:

Router::connect('/Album/:slug/*',array('controller' => 'albums','action' => 'contents'),array('pass' => array('slug')));

for what doesn't match the first. In the 'contents' action of the 'albums' controller, I take care of pagination myself - meaning I retrieve the named parameter 'page'. A URL for the second route would look like this: http://somesite.com/Album/foo-bar/page:2

The Above URL indeed works, but when I try to use the HTML Helper (url,link) to output a url like this, it appends the controller and action to the beginning, like this: http://somesite.com/albums/contents/Album/foo-bar/page:2

Which i don't like. The code that uses the HtmlHelper is as such:

$html->url(array('/Album/' . $album['Album']['slug'] . '/page:' . $next))
Was it helpful?

Solution

See below url it is very help full to you

http://book.cakephp.org/2.0/en/development/routing.html

Or read it

Passing parameters to action

When connecting routes using Route elements you may want to have routed elements be passed arguments instead. By using the 3rd argument of Router::connect() you can define which route elements should also be made available as passed arguments:

<?php
// SomeController.php
public function view($articleId = null, $slug = null) {
    // some code here...
}

// routes.php
Router::connect(
    '/blog/:id-:slug', // E.g. /blog/3-CakePHP_Rocks
    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
<?php
echo $this->Html->link('CakePHP Rocks', array(
    'controller' => 'blog',
    'action' => 'view',
    'id' => 3,
    'slug' => 'CakePHP_Rocks'
));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top