简体   繁体   English

CakePHP时间助手问题

[英]CakePHP Time Helper Problem

I think I might be making a mistake here. 我想我可能在这里犯了一个错误。 I am getting the following error when trying to use a simple function in the time helper in my controller. 尝试在我的控制器中的时间助手中使用一个简单的函数时,我收到以下错误。 I don't get an error when using the same function call in the view. 在视图中使用相同的函数调用时,我不会收到错误。

Below is the error from the controller. 以下是控制器的错误。 Followed by the controller code that is failing. 接下来是失败的控制器代码。 Followed by the view code that is working. 其次是正在运行的视图代码。

Any help is appreciated! 任何帮助表示赞赏!

Error: 错误:

Notice (8): Undefined variable: time [APP/controllers/temp_users_controller.php, line 25] Code $checkTime = $time->gmt(); 注意(8):未定义的变量:time [APP / controllers / temp_users_controller.php,第25行] Code $ checkTime = $ time-> gmt(); Fatal error: Call to a member function gmt() on a non-object in /var/www/studydeck/app/controllers/temp_users_controller.php on line 25 致命错误:在第25行的/var/www/studydeck/app/controllers/temp_users_controller.php中调用非对象上的成员函数gmt()

Controller: 控制器:

class TempUsersController extends AppController { class TempUsersController扩展AppController {

var $name = 'TempUsers';
var $scaffold;
var $components = array('Auth');
var $helpers = array('Time');

function beforeFilter() {
    //list of actions that do not need authentication
    $this->Auth->allow('userCleanUp');

}

//this function will delete TempUser accounts which have not been activated
function userCleanUp() {

    $checkTime = $time->gmt();
    $this->set('checkTime',$checkTime);
}

} }

View: 视图:

echo $time->gmt(); echo $ time-> gmt();

Update: I tried $time = new TimeHelper(); 更新:我试过$ time = new TimeHelper(); I received the error Fatal error: Class 'TimeHelper' not found in /var/www/studydeck/app/controllers/temp_users_controller.php on line 23 我收到错误致命错误:第23行的/var/www/studydeck/app/controllers/temp_users_controller.php中找不到类'TimeHelper'

I do have var $helpers = array('Time') 我有var $ helpers = array('Time')

Also not that echo $time->gmt(); 也不是echo $ time-> gmt(); works in the view with out instantiating time anywhere. 在视图中工作,无需在任何地方实例化时间。

Helpers are thought to be used in views, not in controllers, hence the error. 辅助程序被认为用于视图,而不是控制器,因此错误。 If you really have to use the helper in your controller, you have to instantiate it yourself: 如果你真的必须在你的控制器中使用帮助器,你必须自己实例化它:

$time = new TimeHelper();

You generally don't use a helper from the controller. 您通常不使用控制器中的帮助程序。 The array you assign in the controller is used to load and instantiate the helper classes before the view is parsed and rendered. 您在控制器中分配的数组用于在解析和呈现视图之前加载和实例化辅助类。

To use the helper in your controller you need to make sure the file is loaded ( included ) correctly and that you have an instance of it to work with. 要在控制器中使用帮助程序,您需要确保正确加载(包含)文件,并且您有一个可以使用它的实例。 You can also use many of them statically from within a controller, expecially those helpers that don't need access to the controller object. 您还可以在控制器中静态使用其中许多,特别是那些不需要访问控制器对象的帮助程序。

Cake provides you with a core function that you can use to safely include the files. Cake为您提供了一个核心功能,您可以使用它来安全地包含文件。 It will even handle where the file gets loaded from automagically so you don't need to deal with paths. 它甚至可以处理文件从自动加载的位置,因此您不需要处理路径。

An example to get you started. 一个让你入门的例子。

<?php
    class TempUsersController extends AppController
    {
        public $name = "TempUsers";

        public function userCleanUp( ){
            // include the time helper
            App::import( 'Helper', 'Time' );
            $time = new TimeHelper;
            $checkTime = $time->gmt( );
            $this->set( 'checkTime',$checkTime );
        }
    }
?>

On this line: 在这一行:

 $checkTime = $time->gmt();

The $time variable is not defined and is automatically set to null. $ time变量未定义,并自动设置为null。 The error states that it is not an object (which is correct). 该错误表明它不是一个对象(这是正确的)。 Did you initialize it properly? 你正确地初始化了吗? I cannot see any initialisation done for the $time variable. 我看不到$ time变量的任何初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM