Jay
Jay

Reputation: 2902

CakePHP accessing other controllers

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!

Upvotes: 0

Views: 345

Answers (2)

webbiedave
webbiedave

Reputation: 48897

$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.

Upvotes: 2

Young
Young

Reputation: 8356

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

Upvotes: 0

Related Questions