Jannie Theunissen
Jannie Theunissen

Reputation: 30154

Relocating Cake's default layouts folder

I'm working on a CakePHP 1.2 app where I don't have control over the /app directory and have relocated my views folder to, say /path/to/mycomponent/views and can render my views by setting my controller's default view path

var $viewPath = "mycomponent/views";

I have some layouts (including basic.ctp) under /path/to/mycomponent/views/layouts

Is there a way to render my views inside a specific layout?

Calling this from my controller gives me a page not found error:

function index()
    {
        $this->autoLayout = true;
        $this->pageTitle = 'Browse items';
        $this->render("browse", "basic");
        // also tried: $this->render("browse", "/path/to/mycomponent/views/layouts/basic.ctp");
    }

Upvotes: 1

Views: 1066

Answers (2)

Roshdy
Roshdy

Reputation: 1812

Just for any further reference. I believe in cakePhp 2.x it's to be done this way.

public function beforeRender() { parent::beforeRender();
$this->layout = 'mylayout'; }

which would go to the path: /app/View/Layouts/mylayout.ctp

Note:

If you want to modify the view paths for some controller, add before the controller class declaration:

App::build(array('View' => '/Folder/'));

class ExampleController extends AppController{...}

which will set the path for all rendered view for Example controller to /app/Views/Folder/

so the full example would be:

App::build(array('View' => '/Folder/'));

class ExampleController extends AppController{

public function beforeRender() {
    parent::beforeRender();        
    $this->layout = 'mylayout';
}  


public function index() {
    $this->render('Home');
    //This will load view: "/app/Views/Folder/Home.ctp"
    //From layout: "/app/Views/Layouts/mylayout.ctp"
} 

}

Upvotes: 0

Jannie Theunissen
Jannie Theunissen

Reputation: 30154

Finally got it to work by adding a path to the viewPaths array of the app configuration. This code goes in you controller constructor or in the override beforeFilter metod:

$config = Configure::getInstance();
$config->viewPaths[] = " /path/to/mycomponent/views/";

Now I can just specify the following:

$this->layout = 'basic';

And my controller's views render in the basic.ctp layout.

Upvotes: 1

Related Questions