zoukankan      html  css  js  c++  java
  • zend 动作控制器

    动作控制器核心Zend_Controller_Action是一个抽象类,在自己实际的控制器中需要将他子类化。Zend_Controller的路由和派遣处理将在你的类里自动发现任何以'Action'结尾的方法作为潜在的控制器动作。
    在控制器中,可以重写一个public的init()方法,该方法再Zend_Controller_Action构造方法中最后调用

    缺省行为
    默认,前端控制器激活了ViewRenderer动作助手。通过下面方法之一,可以在动作控制器里禁止它

     1 class FooController extends Zend_Controller_Action
    2 {
    3 public function init()
    4 {
    5 // 只是局部控制器;当初始化加载时,对这个控制器的所有动作有效:
    6 $this->_helper->viewRenderer->setNoRender(true);
    7
    8 // 全局:
    9 $this->_helper->removeHelper('viewRenderer');
    10
    11 // 也是全局,但需要和本地版本协作,以便繁殖这个控制器:
    12 Zend_Controller_Front::getInstance()->setParam('noViewRenderer', true);
    13 }
    14 }

    initView()、 getViewScript()、render()和renderScript() 都代理 ViewRenderer,除非助手不在助手经纪人(broker)里或noViewRenderer标志被设置。
    也可以通过设置ViewRenderer的noRender标记,可以简单地为一个独立的视图禁止解析

    1 class FooController extends Zend_Controller_Action
    2 {
    3 public function taqAction()
    4 {
    5 // disable autorendering for this action only:
    6 $this->_helper->viewRenderer->setNoRender();
    7 }
    8 }

    访问器
    •请求对象:getRequest()可用来读取调用动作请求对象。
    •响应对象: getResponse()可用来读取收集最终响应的响应对象。一些典型的调用看起来象这

    1 $this->getResponse()->setHeader('Content-Type', 'text/xml');
    2 $this->getResponse()->appendBody($content);

    •调用参数:前端控制器可能把参数传给路由器、派遣器和动作控制器。为了读取这些参数,可使用getInvokeArg($key);另外,用getInvokeArgs()读取整个参数列表。
    •请求参数:请求对象手机请求参数,如任何_GET 或 _POST 参数,或者指定在URL的路径信息里的用户参数。为了读取这些参数,可使用_getParam($key)_getAllParams()。也可以用_setParam()来设置请求参数;当转发到另外的动作时这很有用。
    用_hasParam($key)来测试是否一个参数存在(对逻辑分支有用)。
    注意: _getParam()可带有一个可选的第二个参数,如果它不是空的,就包含一个缺省的值。用它在读取值之前来消除对_hasParam() 的调用:

    通过重写__call()方法处理不存在的动作
    如果控制器的请求包括一个未定义的动作方法,Zend_Controller_Action::__call()将被调用。__call()当然是PHP中用来重载方法的魔术方法。
    缺省地,这个方法抛出一个Zend_Controller_Action_Exception 来表明在控制器里没有发现要求的方法。如果要求的方法以'Action'结尾,就假设一个动作被请求并且不存在;这样的错误导致带有代码为 404 的异常。所有其它方法导致带有代码为 500 的异常。这使你很容易地在错误句柄里区分是页面没有发现还是程序错误。
    如果想执行其它操作,你应该重写这个函数。例如,如果你想显示错误信息,可以象下面这样来写:

     1 class MyController extends Zend_Controller_Action
    2 {
    3 public function __call($method, $args)
    4 {
    5 if ('Action' == substr($method, -6)) {
    6 // If the action method was not found, render the error
    7 // template
    8 return $this->render('error');
    9 }
    10
    11 // all other methods throw an exception
    12 throw new Exception('Invalid method "'
    13 . $method
    14 . '" called',
    15 500);
    16 }
    17 }

    另外的可能性就是你可能想转发到缺省控制页面:

    class MyController extends Zend_Controller_Action
    {
    public function indexAction()
    {
    $this->render();
    }

    public function __call($method, $args)
    {
    if ('Action' == substr($method, -6)) {
    // If the action method was not found, forward to the
    // index action

    return $this->_forward('index');
    }

    // all other methods throw an exception
    throw new Exception('Invalid method "'
    . $method
    . '" called',
    500);
    }
    }
  • 相关阅读:
    【转-整理】Eclipse中Maven项目创建
    Python错误——failed to execute pyi_rth_pkgres
    linux使用——创建用户、设置密码、设置SSH远程秘钥登录
    Javascript数组——some()、every()、forEach()遍历区别
    linux使用——nohup不起作用
    IDEA操作——Intellij IDEA 无法找到引入的包和类
    mysql中批量update数据
    获取中国标准的本周日期
    tomcat下配置https环境
    mysql中创建远程用户并授权,得以在本地代码中运行远程数据库
  • 原文地址:https://www.cnblogs.com/gbyukg/p/2185268.html
Copyright © 2011-2022 走看看