tags:

views:

356

answers:

2

Hello,

Can you tell me how to use a controller for home page because i'm trying to put a model's data in home.ctp (homepage view) with <?php $this->user->find() ?>but it returns

Notice (8): Undefined property: View::$user [APP\views\pages\home.ctp, line 1]

Thanks

A: 

All you really need to do is create a new controller if that's the direction you want to go. If this is the only statement you have that requires data access, it might be worth faking it in only this method of the PagesController. For example, one of my projects' homepages is 99% static save for a list of featured events. Rather than move everything out to a new controller or even loading my Event model for the entire PagesController (where it's not needed), I just applied this solution in PagesController::home():

$attractions = ClassRegistry::init ( 'Attraction' )->featured ( 10 );

Works great. If your page is more dynamic than mine, though, it may well be worth routing your homepage through a different controller (one that is more closely related to the data being displayed).

Rob Wilkerson
A: 

You should check out the cookbook; it has some solid CakePHP tutorials, at http://book.cakephp.org/

You haven't really provided alot of information, but my guess is your Controller uses a model 'User', and you're putting $this->user->find() in your view, when it should be in your controller. In your controller's action you'll want/need to do something like this...

Users_Controller extends AppController {
    function index() {
        $arrayOfUsers = $this->User->find(...);
        $this->set('users', $arrayOfUsers);
    }
}

You can then - in your View - access 'users' like so:

pre($users);

... since you used the Controller method set() to send a variable $users to the view.

LeguRi
This should be UsersController (no underscore), and the debug function is pr() not pre()
duckyflip
Haha, ya, that's my bad. When I work in PHP without Cake, I always define my own version of pr which I call pre since I like function names to have at least one vowel :)
LeguRi