How to prevent controller and action from coming up in URL in cakephp?
Asked Answered
T

1

7

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))
Tellez answered 11/9, 2012 at 6:30 Comment(0)
W
2

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'
));
Worthless answered 14/9, 2012 at 11:46 Comment(1)
I was forced to use a different solution. Still marking your answer as correct.Tellez

© 2022 - 2024 — McMap. All rights reserved.