Gilko
Gilko

Reputation: 2407

CakePHP : having different layout files for backend and frontend

Is it possible to have 2 different layout files in the app > view > Layouts folder. The reason that I'm asking this is that I already have my backoffice, but don't know how to proceed in making the structure for the frontoffice.

The routes of the views in my backoffice use the prefix admin and use the default.ctp as layout. I actually want that file to be admin_default.ctp so I can use the default.ctp for my frontoffice views.

My question now is how can I separate those layout files. What I want to achieve is that some controllers/views use default.ctp (front) and other controllers/views use the admin_default.ctp (back). Is this a good approach or is it better to consider an alternative?

Upvotes: 0

Views: 834

Answers (2)

Dave
Dave

Reputation: 29141

In your AppController's beforeFilter, just check if it's admin prefix, and set the layout accordingly:

class AppController extends Controller {

public function beforeFilter() {
    if($this->params['prefix'] == 'admin' && $this->name !== 'CakeError') {
        $this->layout = "admin"; // set the layout
    }
}

//...

Another nice thing you can do after determining it's the admin is is force SSL (often a good idea for back-ends):

$this->Security->requireSecure();  // inside the above if block

Upvotes: 1

Farshad
Farshad

Reputation: 1485

simply you can use $this->layout to specify your custom layout in controller.

this article may be useful for your question

Upvotes: 2

Related Questions