Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP - Quick way to get /controller/action path?

Is there a Controller property that will allow me to get just /controller/action from the URL without any additional parameters there might be?

At the moment I am having to join $this->name . '/' . $this->action.

like image 814
BadHorsie Avatar asked Jul 27 '11 12:07

BadHorsie


2 Answers

You don't want to construct the string /users/login, you want the URL that corresponds to the login action of your users controller (for example). That is not necessarily the same as /users/login, and you should not hardcode it!

To get a URL that will lead to a controller action, use reverse routing:

Router::url(array('controller' => 'users', 'action' => 'login'));
//or
Router::url(array('controller' => $this->name, 'action' => $this->action));

Yes, that's even longer, but it's the correct way to do it. If one day you decide you want the login URL to be /login or /members/entrance instead of /users/login, you only need to define an appropriate route in routes.php without rewriting all your hardcoded links.

like image 200
deceze Avatar answered Sep 28 '22 15:09

deceze


$this->here

Available in view and controller. Minor note: It's getting removed in 2.0.

like image 26
Dunhamzzz Avatar answered Sep 28 '22 17:09

Dunhamzzz