Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In View(CakePHP), the proper way to get current controller?

Tags:

cakephp-2.0

In View, I can get action by using

$this->action 

But, I cannot get controller name by

$this->controller 

What is the proper way to get current controller in View?

like image 779
Hao Avatar asked Oct 23 '12 15:10

Hao


2 Answers

Use $this->params['controller'] to get the current controller.

You can do a debug($this->params) to see other available variables.

like image 140
Wa0x6e Avatar answered Oct 02 '22 23:10

Wa0x6e


You can get controller like this:

echo "<pre>controller:".$this->request->params['controller']."</pre>"; 

Although $this->params is shorter, $this->request->params is more autocomplete friendly. You can check the autocomplete options from this question: PHPStorm autocomplete for CakePHP custom helpers in view files

Other data about request can be taken like this:

echo "<pre>action:".$this->request->params['action']."</pre>";  echo "<pre>request:"; print_r( $this->request ); echo "</pre>";  echo "<details><summary>this:</summary><pre>";        print_r( $this ); echo "</pre></details>"; 

Edit:
As of CakePHP 3 $this->params shortcut is removed. So you should user $this->request->params['controller'] for CakePHP 3.
http://book.cakephp.org/3.0/en/appendices/3-0-migration-guide.html#id2
Also note that first character of controller is uppercase. It was lowercase in Cakephp 2.

like image 44
trante Avatar answered Oct 03 '22 00:10

trante