助手在视图中不可用

Helper not available in the view

我正在从 1.3 迁移到 2.0.6,遇到以下问题:

Notice (8): Undefined variable: session [APP/View/Users/login.ctp, line 2]

Fatal error: Call to a member function flash() on a non-object in login.ctp on line 2

为什么访问会话助手时出现问题?

这不是使用助手的方法

Notice (8): Undefined variable: session [APP/View/Users/login.ctp, line 2]

Fatal error: Call to a member function flash() on a non-object in login.ctp

虽然它不在问题中,但您的 login.ctp 模板看起来像这样:

<?php
echo $session>flash(); # line 2

那是不是 how helpers are expected to be used in CakePHP 2.x:

you can use [the helper] in your views by accessing an object named after the helper:

<!-- make a link using the new helper -->
<?php echo $this->Link->makeEdit('Change this Recipe', '/recipes/edit/5'); ?>

您要查找的代码是:

# echo $session->flash(); # wrong
echo $this->Session->flash(); # right

另请注意,Session 需要在 controller $helpers array 中。

在 1.2 和更早版本的 CakePHP 中,帮助器应该是变量,this changed but was still supported in 1.3. If your 1.3 CakePHP application has been using helpers in this way, it has been relying on backwards-compatibility with earlier versions. In 2.0 helpers are not variables, and only possible to access as a view class property. Be sure to read the migration guides - 有关更改内容的更多信息。