【CakePHP1.3 】Controller的方法

来源:互联网 发布:mysql gtid复制原理 编辑:程序博客网 时间:2024/05/22 03:42

Controller的方法

尽管本小节会介绍Cake模型中大多数频繁使用的方法,但也请记住 http://api.cake.org 可以获得完整的API参考。

 

与你的view进行交互

set

    string $var    mixed $value   

这个方法是你的view从controller得到数据的主要方法。你可以用它传递任何数据:单一变量值,整个数组,等等。一但调用了set(),相应的变量就可以在view里访问到了:在controller里调用set(‘color’, ‘blue’)使得变量$color在view里变得可用了。

validateErrors

返回在一次不成功的保存中生成的错误个数。

validate

根据一个model预定义的有效性规则验证该model的数据。关于数据验证的的更多内容,请参见第11章。

render

    string $action    string $layout    string $file   

你也许不会经常使用这个方法,因为render方法是在controller action结束时自动被调用的,输出按action名字命令的view。同时,你也可以在controller逻辑里的任意位置调用来这个方法输出视图。

 

用户重定向

redirect

    string $url   

通过此方法告诉你的用户应该继续访问什么地方。这里传入的URL参数可以是一个Cake内部URL,也可以是一个完整的URL(http://...)。

flash

    string $message    string $url    int $pause   

该方法将在你的flash页面(该页面位于app/views/layouts/flash.thtml)上显示提示信息[$message],停顿时间若干[$pause]秒,然后重定向用户到指定的Url[$url]。 Cake的redirect()和flash()方法并不包含exit()的调用。如果你希望自己的应用程序在redirect()或flash()之后停止继续执行,你需要自己在这些方法之后立即调用exit()。你也可能想要return()而不是exit(),依你的具体情况而定(例如,你需要执行某些回调)。

 

controller中的回调函数

Cake的controller特别提供了许多回调方法,你可以用它们在一些重要的controller方法之前或者之后插入一些逻辑。如果要利用这些功能,请在你的controller中用这里描述的参数和返回值定义这些方法。

beforeFilter

在每个controller action调用前执行。它是一个检查当前sessoin状态和检查用户角色的好地方。

afterFilter

在每个controller action调用后执行。

beforeRender

在controller逻辑之后,并且在输出视图之前被调用。

 

其他有用的方法

尽管这些方法是Cake的Object类的一部分,他们在controller里仍然可用。

requestAction

    string $url    array $options       

这个方法可以在任意位置调用某个controller的action并且返回render后的视图。$url是一个Cake URL(/controllername/actionname/params)。如果$extra(译注:该方法里并没有出现$extra参数,这里是不是应该为$options?)数组包含’return’,这个action的AutoRender将会自动被设置为true。 你可以用requestAction从另一个controller action获取数据,也可以从另一个controller获取整个输出后的视图。
首先,从一个controller获取数据是很简单的。你只需在需要数据的地方使用requestAction方法。

// Here is our simple controller:        class UsersController extends AppController    {        function getUserList()        {            $this->User->findAll();        }    }  
设想我们需要创建一个简单的表格来显示系统里的用户。我们不用在另一个controller里重复编码,而只需要用requestAction()来调用UsersController::getUserList()就能得到用户数据。
    class ProductsController extends AppController        {            function showUserProducts()            {                $this->set('users', $this->requestAction('/users/getUserList'));                        // Now the $users variable in the view will have the data from                // UsersController::getUserList().            }        }    

如果在你的程序里,有一个频繁使用而又非静态的html元素(element),你也许会想使用requestAction()来将它插入到任意view里边。实际上我们不只是想要从UsersController::getUserList获取数据,还想要在另一个controller里边render这个action的view(这个view也许已包含了表格)。这样的方式可以避免重复编写view的代码。

 

    class ProgramsController extends AppController        {            function viewAll()            {                $this->set('userTable', $this->requestAction('/users/getUserList', array('return')));                        // Now, we can echo out $userTable in this action's view to                // see the rendered view that is also available at /users/getUserList.            }        }    

请注意这里用requestAction()调用的action用的是一个空的布局(layout)-这样的话你就不必担心布局与布局之间嵌套带来的麻烦了。

在使用AJAX的情况下,当在一个AJAX调用之前或调用中间想从一个view生成一个html元素,requestAction()方法也是很有用的。


log

    string $message    int $type = LOG_ERROR       

你可以使用这个方法来记录web应用程序里发生的不同事件。所有生成的log可以在Cake的/tmp目录下找到。
如果$type的值为PHP常量LOG_DEBUG,消息将作为debug信息被写入log。对于其他任何值,消息作为error信息被写入log。

    // Inside a controller, you can use log() to write entries:                $this->log('Mayday! Mayday!');                //Log entry: 06-03-28 08:06:22 Error: Mayday! Mayday!                $this->log("Look like {$_SESSION['user']} just logged in.", LOG_DEBUG);                //Log entry: 06-03-28 08:06:22 Debug: Looks like Bobby just logged in.  

postConditions

    array $data       

一个你可以用其将传入的$this->data格式化成model条件数组(conditions array)的方法。
例如,你有一个人员搜索表单:

// app/views/people/search.thtml: 

提交包含这个元素的表单将会产生以下$this->data数组:

Array(    [Person] => Array        (            [last_name] => Anderson        ))       

在这里,我们就可以用postConditions()来格式化这些数据以便于在model里使用:

    // app/controllers/people_controller.php:                $conditions = $this->postConditions($this->data);                // Yields an array looking like this:                Array        (            [Person.last_name] => Anderson        )                // Which can be used in model find operations:                $this->Person->findAll($conditions);  

























原创粉丝点击