English 中文(简体)
cakePHP: how to combine two or more application views on one cakePHP layout page?
原标题:

Using cakePHP my goal is to combine the index view of two or more controllers in one layout page.

Example: I have controllers for: news, events, links. I want to show the last five entries from each table in one layout page. Also, when one of the links from the views is selected it should take the user to the respective view for that record.

I have read through the books section on views but don t see how making a view into an element would accomplish this.

What confuses me is how to combine from three separate controller/views into one layout?

Thanks

最佳回答

Create methods in your News, Event and Link models for fetching the last 5 records. Then in your controller either include the models in the Controller::uses property, or in the action use ClassRegistry::init() to get access to the model, e.g.

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, keeping your application DRY.

In your my_action.ctp view, and indeed many other views, just render the elements e.g.

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

Your elements can then just iterate over the $news (or whatever) variable displaying the items with links to the view actions in their respective controllers.

Just because a controller matches a model, doesn t mean you can t use other models in it.

问题回答

First I would say that views and controllers are not necessarily tied together -- Cake will implicitly add the view specified by the file heirarchy / naming convention, but this doesn t necessarily have to be the case. So try to think of the views as decoupled from the controller (which is one of the main purposes for using the MVC architecture).

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

Basically, just abstract the code you want to display into elements, then insert those elements into the desired layouts.

You can set up your controller to use the RequestHandler and then make your view elements capable of fetching their own data from separate controllers in your application.

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

One thing to keep in mind is that the controller actions you are calling should account for caching their own data so you don t do unnecessary database queries.





相关问题
PHP Framework: Ebay Like Site

I am going to be builiding a site like ebay - with all the features of ebay. Please note my payment method is limited to paypal. What would be the best PHP framework to use to build this quickly, ...

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

Using DISTINCT in a CakePHP find function

I am writing a CakePHP 1.2 app. I have a list of people that I want the user to be able to filter on different fields. For each filterable field, I have a drop down list. Choose the filter ...

Assistance with CakePHP model relationships

How would I represent the following in a CakePHP model? Product ======= product_id .... Cart ==== cart_id .... Carts_Products ============== cart_id product_id quantity

Prevent controller from trying to autoload model

I am a beginning Cake user and trying to do some work on an already existing application. Running into a problem when I create a new controller. I have created StoreController and when I try to call ...

热门标签