如何在CakePHP中实现默认视图?

时间:2013-10-06 14:03:43

标签: cakephp

在CakePHP中,Controller的每个方法都有自己的View,视图模板文件是方法的名称。

class DataController extends AppController 
{
  public function one()
  {
    // will render one.ctp
  }

  public function two()
  {
    // will render two.ctp
  }
}

根据API文档,Controller的$view属性指定了要呈现的视图。所以我应该能够为控制器的所有方法指定一个默认的视图文件,比如说all.ctp

class DataController extends AppController 
{
  public $view = 'all';

  public function one()
  {
    // should render all.ctp
  }

  public function two()
  {
    // should render all.ctp
  }
}

但是这不起作用,CakePHP忽略$view属性并继续查找与方法同名的模板文件。

有没有办法拥有默认视图而无需在每个Controller的方法中插入$this->render('all');

1 个答案:

答案 0 :(得分:1)

将在Controller::setRequest()中覆盖该值,该值在控制器类constructor中被调用。

您可以使用控制器beforeFilter() callback来设置值:

public function beforeFilter()
{
    parent::beforeFilter();
    $this->view = 'all';
}
相关问题