tags:

views:

399

answers:

2

I am working on a CMS using CakePHP and I want to create a dynamic menu which is the same on all the pages the user can access. So I figured out to create in the layout (since it shared among so many pages and view) but I don't seem to know how to acces the model and get the data from the database to construct the menu. any help is appreciated.

+6  A: 

That's because for proper MVC separation you're not supposed to access the Model from the View. The only part with access to data should be the Controller (via the Model), which hands it down to the View, which simply displays the data.

As such, using a beforeFilter callback in your global AppController to set() the data is probably the best choice.

In emergencies you can always access anything from anywhere by loading an instance of the needed Class using ClassRegistry::init, but you really shouldn't.

deceze
+3  A: 

An alternative could be requestAction, it allows you to call controller actions from views/layouts, and in those actions you can then access the desired model(s).

dhofstet