Kim Bit
Kim Bit

Reputation: 13

multiple view in one layout

I know how to use elements to render multiple views in on layout.

What I want to ask is that say I have a mutiple controllers for multiple things. For instance, UsersController, ArticlesController, and BooksController. Say I want to pull the five most recent data from each controllers and put them in one layout like in a home page.

How do you do that?

Upvotes: 1

Views: 158

Answers (2)

Jacek B Budzynski
Jacek B Budzynski

Reputation: 1413

PagesController.php

class PagesController extends AppController {
    public function initialize() {
        parent::initialize();

        $this -> loadModel('Users');
        $this -> loadModel('Articles');
        $this -> loadModel('Books');

    }

    public function home() {

        $users = $this -> Users -> find('all') -> order(['Users.id' => 'desc']) -> limit(5);
        $articles = $this -> Articles -> find('all') -> order(['Articles.id' => 'desc']) -> limit(5);
        $books = $this -> Books -> find('all') -> order(['Books.id' => 'desc']) -> limit(5);

        $this -> set(compact('users', 'articles', 'books'));
    }  
}

Now you can see last 5 Users, Articles and Books in Pages/Home.ctp

Upvotes: 1

Shahadat Atom
Shahadat Atom

Reputation: 334

You should pull data in different variable for different controller. and use these variables in your layout.

Upvotes: 0

Related Questions