tags:

views:

95

answers:

2

CakePHP Newbie :)

I am having trouble accessing another controller and passing that data to a view in one of my controllers:

In controllers/landings_controller.php:

var $uses = 'User';

function home() {
    $userdata = $this->User->read();
    $this->set(compact('userdata'));
}

In views/landings/home.ctp:

<?php 
    echo $this->userdata;       
?>

When accessing /landings/home I get the following error:

Notice (8): Undefined property: View::$userdata [APP/views/landings/home.ctp, line 38]

I don't know what I am doing wrong. Any help? Thanks!

+2  A: 
$this->set('userdata', $userdata);

Compact returns a single array. $this->set expects two parameters.

http://book.cakephp.org/view/57/Controller-Methods

Correction: set does in fact accept associative arrays (thanks Daniel Wright). Read below about using variables in views.

Also, variables are placed in scope -- not attached as members -- so you wouldn't do this in the view:

<?php echo $this->userdata ?>

but, rather:

<?php echo $userdata ?>

Assuming $userdata is a scalar, of course.

webbiedave
In fact, `$this->set` does work as the original poster suggests. If an array is supplied as a parameter, `$this->set` uses the array's keys and values instead.
Daniel Wright
Gotcha. Very nice.
webbiedave
A: 

I think using compact is fine.You need know more about set().

SpawnCxy