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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章