STEJ
STEJ

Reputation: 143

How to implement a default view in CakePHP?

In CakePHP each method of a Controller has its own View and the view template file is the name of the method.

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

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

Accourding to the API documentation there is a $view property of the Controller that specifies the view to render. So I should have the ability to specify a default view file, say all.ctp, for all methods of a controller

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

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

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

However this does not work and CakePHP ignores the $view property and continues to look for the template file of the same name as the method.

Is there a way to have a default view without having to insert $this->render('all'); in each of the Controller's methods?

Upvotes: 0

Views: 424

Answers (1)

ndm
ndm

Reputation: 60463

The value is going to be overridden in Controller::setRequest() which is being called in the controllers class constructor.

You could use your controllers beforeFilter() callback instead to set the value:

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

Upvotes: 1

Related Questions