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

前端 未结 8 849
伪装坚强ぢ
伪装坚强ぢ 2021-02-02 07:36

In View, I can get action by using

$this->action

But, I cannot get controller name by

$this->controller
8条回答
  •  -上瘾入骨i
    2021-02-02 08:11

    You can get controller like this:

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

    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 "
    action:".$this->request->params['action']."
    "; echo "
    request:"; print_r( $this->request ); echo "
    "; echo "
    this:
    "; 
          print_r( $this ); echo "
    ";

    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.

提交回复
热议问题