0

When you want to have a link like

http://www.domain.com/users/54/John

In Cakephp you have to rewrite it to:

http://www.domain.com/users/index/54/John

If you don't do this cakephp complains that within the users controller there is no action called 54. Is there no other way around this? Like to tell cakephp that when the second variable in the url is a number (so in this case 54), to refer it to the index function:

public function index($userId = null).

Another example is the url of this question. If it was cakephp, the url would look like:

http://stackoverflow.com/questions/index/18547494/avoid-having..

Is it in cakephp possible to do something like this?

http://stackoverflow.com/questions/18547494/avoid-having..

I hope this makes sense.

1
  • You should be using query strings and probably some custom routing. This avoids this issue completely - in a clean and future proof way. Commented Aug 31, 2013 at 17:53

3 Answers 3

1
Router::connect('/users/:id/:name', array(
    'controller' => 'users',
    'action' => 'index'
), array('pass' => array('id', 'name')));

Your controller action code should be like this:

public function index($id = null, $name = null) {
    // your code
}

In your view you can define link as follows to access the url

echo $this->Html->link('view', array(
    'controller'  => 'users',
    'action' => 'index'
    'id' => $yourid,
    'name' => $yourname
))
Sign up to request clarification or add additional context in comments.

Comments

0

You'll need to use the id key in the routing array when creating links. Something like this:

array(
    'controller'=>'users', 'action'=>'index', 'id' => 54
}

1 Comment

then it will still put index in the url
0

You should have have this:

Router::connect(
   '/:controller/:id',
   array('action' => 'view'),
   array('id' => '[0-9]+')
);

Read: Routing in CakePHP

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.