Zend Framework: Action Helper

Zend Framework 默认包含了许多action helper:

  • AutoComplete用来自动response AJAX autocompletion;
  • ContextSwitchAjaxContext服务于你的actions的交替response格式;
  • FlashMessenger用来处理session flash messages;
  • Json用来encoding和发送JSON response;
  • Redirector提供不同的实现方法来重定向到你的应用中的不同的页面(这里也就是说$this->redirect());
  • ViewRenderer用来自动化设置view object给你的controller和rendering views。

ActionStack

*ActionStack*helper允许你push requests给ActionStack front controller plugin,非常有效的帮助你在执行request时创建一个actions队列。这个helper允许你通过指定一个新的request或action-controller-module set的方式添加actions。

My comprehension

也就是说这个ActionStack可以用来控制action的访问的,也就是在controller里面的Action方法中让它也能用上其他action里面的功能,在功能都一样的情况下,就没必要多写一遍,使用这个方法就可以很好来在action里请求别的action。下面上代码(Zend文档里面的)
Example#1Adding a Task Using Action, Controller and Module Names
这就和Zend_Controller_Action::_forward()是一样的

class FooController extends Zend_Controller_Action
{
    public function barAction()
    {
        // Add two actions to the stack
        // Add call to /foo/baz/bar/baz
        // (FooController::bazAction() with request var bar == baz)
        $this->_helper->actionStack('baz',
                                    'foo',
                                    'default',
                                    array('bar' => 'baz'));

        // Add call to /bar/bat
        // (BarController::batAction())
        $this->_helper->actionStack('bat', 'bar');
    }
}

Example #2 Adding a Task Using a Request Object
有时候OOP特性中的request object非常有意义;你也可以通过object的方法发送给ActionStack

class FooController extends Zend_Controller_Action
{
    public function barAction()
    {
        // Add two actions to the stack
        // Add call to /foo/baz/bar/baz
        // (FooController::bazAction() with request var bar == baz)
        $request = clone $this->getRequest();
        // Don't set controller or module; use current values
        $request->setActionName('baz')
                ->setParams(array('bar' => 'baz'));
        $this->_helper->actionStack($request);

        // Add call to /bar/bat
        // (BarController::batAction())
        $request = clone $this->getRequest();
        // don't set module; use current value
        $request->setActionName('bat')
                ->setControllerName('bar');
        $this->_helper->actionStack($request);
    }
}
发布了34 篇原创文章 · 获赞 4 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/Tianyi_liang/article/details/60959097