PHP头条
热点:

cakephp中的controller变量


Controller 变量

操作Controller里的少量变量,可以让你最大程度的使用Cake的额外功能:

$name

PHP4没有把当前的驼峰格式的类名给我们。如果你有问题,可以使用此变量来设置正确的驼峰格式的类名。

$uses

你的Controller是否使用多个model呢?FragglesController会自动加载$this->Fraggle,但是如果你也想访问$this->Smurf,试试将下面的东东加到你的controller中:

var $uses = array('Fraggle','Smurf');

请注意你是如何在$use数组中包含Fraggle model的,虽然在之前它也自动可用。

$helpers

使用本变量可以让controllerhelper加载到它的view中去。HTML helper会自动加载,但是你可以使用本变量指定其他的:

var $helpers = array('Html','Ajax','Javascript');

记住,如果你打算用它的话,你需要在$helpers数组中包含HtmlHelper。一般它是缺省可用的,但是如果你定义了没有它的$helpers,在你的view中你会得到错误信息。

$layout

将本变量设置为你想在controller中使用的布局名。

$autoRender

将本变量设置为false,会自动停止actionrender

$beforeFilter

如果你想让你的一点点代码在每次的action调用中都运行(和任何动作运行之前),使用$beforeFilter.此东西对访问控制来说真的非常好-你可以在任何动作发生前检查用户的权限。将此变量设置为一个包含controller 动作的数组。可以如下运行:

class ProductsController extends AppController

{

    var $beforeFilter = array('checkAccess');

 

    function checkAccess()

    {

        //Logic to check user identity and access would go here....

    }

 

    function index()

    {

        //When this action is called, checkAccess() is called first.

    }

}

$components

$helpers$uses一样。此变量用来加载你需要的组件:

var $components = array('acl');[2]

 

转自:http://www.cnblogs.com/confach/articles/562772.aspx

www.phpzy.comtrue/phpkj/10548.htmlTechArticlecakephp中的controller变量 Controller 变量 操作 Controller 里的少量变量,可以让你最大程度的使用 Cake 的额外功能: $name PHP4 没有把当前的驼峰格式的类名给我们。如果你有问题,可以使用此...

相关文章

相关频道:

PHP之友评论

今天推荐