[英]CakePHP accessing other controllers
CakePHP Newbie :) CakePHP新手:)
I am having trouble accessing another controller and passing that data to a view in one of my controllers: 我在访问另一个控制器并将该数据传递到我的一个控制器的视图时遇到麻烦:
In controllers/landings_controller.php
: 在
controllers/landings_controller.php
:
var $uses = 'User';
function home() {
$userdata = $this->User->read();
$this->set(compact('userdata'));
}
In views/landings/home.ctp
: 在
views/landings/home.ctp
:
<?php
echo $this->userdata;
?>
When accessing /landings/home
I get the following error: 访问
/landings/home
,出现以下错误:
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. Compact返回单个数组。 $this->set expects two parameters.
$ this-> set需要两个参数。
http://book.cakephp.org/view/57/Controller-Methods http://book.cakephp.org/view/57/Controller-Methods
Correction: set does in fact accept associative arrays (thanks Daniel Wright). 校正: set实际上接受关联数组(感谢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. 当然,假设$ userdata是一个标量。
我认为使用compact
很好,您需要了解有关set()的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.