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/landi开发者_运维百科ngs/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!
$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.
I think using compact
is fine.You need know more about set().
精彩评论