CakePHP时间助手问题 [英] CakePHP Time Helper Problem

查看:138
本文介绍了CakePHP时间助手问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我可能在这里犯了一个错误。
当我试图在我的控制器中的时间帮助中使用一个简单的函数时,得到以下错误。在视图中使用相同的函数调用时,我不会收到错误。

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.

任何帮助是感谢!

错误:

*注意(8):未定义的变量:time [APP / controllers / temp_users_controller.php,第25行]
checkTime = $ time-> gmt();
致命错误:在第25行的/var/www/studydeck/app/controllers/temp_users_controller.php中的非对象上调用成员函数gmt()*

*Notice (8): Undefined variable: time [APP/controllers/temp_users_controller.php, line 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*

控制器

class TempUsersController扩展AppController {

class TempUsersController extends 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);
}

}

查看:

echo $ time-> gmt();

echo $time->gmt();


我试过$ time = new TimeHelper();
我收到错误
致命错误:在第23行的/var/www/studydeck/app/controllers/temp_users_controller.php中找不到类'TimeHelper'

Update: I tried $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

我也有var $ helpers = array('Time')

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

也不是echo $ time-> gmt

Also not that echo $time->gmt(); works in the view with out instantiating time anywhere.

推荐答案

您通常不使用控制器中的帮助器。
在控制器中分配的数组用于在视图被解析和渲染之前加载和实例化助手类。

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为您提供了一个核心功能您可以使用安全地包括文件。它甚至会处理文件从自动加载的位置,所以你不需要处理路径。

Cake provides you with a core function that you can use to safely include the files. It will even handle where the file gets loaded from automagically so you don't need to deal with paths.

一个让你开始的例子。

<?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 );
        }
    }
?>

这篇关于CakePHP时间助手问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆