简体   繁体   中英

CakePHP: How to show the same query data in all my views, directly in the layout file

I'm creating a website with CakePHP; the problem now is that I want to show in the website footer (that will be visible on all the pages), the result of a query (eg "Most popular products").

What is the correct way with CakePHP to achieve this?

At the moment I created a mostPopularList() in my Product controller and a most-popular-list.ctp view that only outputs an <ul> list, thinking that then I could include the output of this file in my layout ( default.ctp ), but I didn't find the CakePHP way to do that.

Thanks!

Use a view cell or call the query in the AppControllers beforeRender() callback and set it there to the view. Taken from the documentation:

View cells are small mini-controllers that can invoke view logic and render out templates. They provide a light-weight modular replacement to requestAction(). The idea of cells is borrowed from cells in Ruby, where they fulfill a similar role and purpose.

If this is needed on really every single page I would probably go for the beforeRender() callback, easy to do and change globally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM