CakePHP: how to combine two or more kinds of applications on one page of cakePHP layout?

Using cakePHP, my goal is to combine the index view of two or more controllers on the same layout page.

Example: I have controllers for: news, events, links. I want to show the last five records from each table on one page of the layout. In addition, when one of the links from the views is selected, the user must navigate to the corresponding view for this entry.

I have read the views book section, but can't see how the presentation in the element will do this.

What bothers me how to combine from three separate controllers / representations into one layout?

thanks

+4
source share
3 answers

Create methods in news, events, and link models to get the last 5 entries. Then, in your controller, models in the Controller :: uses property are turned on, or in action use ClassRegistry :: init () to access the model, for example.

function my_action() { $news = ClassRegistry::init('News')->getRecent(); $events = ClassRegistry::init('Event')->getRecent(); $links = ClassRegistry::init('Link')->getRecent(); $this->set(compact('news', 'events', 'links')); } 

You can then call these model methods from any controller action while saving the DRY application.

In your view my_action.ctp and even in many other views just draw the elements, for example.

 // my_action.ctp <?php echo $this->element('recent_news'); echo $this->element('recent_events'); echo $this->element('recent_links'); ?> 

Then your elements can simply iterate over the $ news variable (or any other) that displays elements with links to "view" actions in their respective controllers.

Just because the controller matches the model does not mean that you cannot use other models in it.

+5
source

First, I would say that views and controllers are not necessarily related to each other - Cake will implicitly add the view specified in the agreement on the file / file naming hierarchy, but this does not have to be so. Therefore, try to think of representations as detached from the controller (which is one of the main goals of using the MVC architecture).

Assuming that your three views (A, B, C) are exactly the way you want to copy them, put them in an element (which is just a view file located in a special directory APP / views / elements /). Now you can use them in layouts or other views by simply calling $ this-> element ('elementName', array ('options')).

Basically, just draw the code you want to display into the elements, and then paste these elements into the desired layouts.

+5
source

You can configure your controller to use RequestHandler, and then make your presentation elements capable of collecting their own data from individual controllers in your application.

This link explains it better than I can http://bakery.cakephp.org/articles/view/creating-reusable-elements-with-requestaction

It should be borne in mind that the actions of the controller that you invoke must consider caching your own data, so you do not make unnecessary queries to the database.

+2
source

All Articles