简体   繁体   English

在Zend Framework 1.12中,如何避免采取行动?

[英]In Zend Framework 1.12, how to avoid getting an action?

I have an action method that only triggers when post data to it. 我有一个动作方法,只有在向其发布数据时才触发。 So I add some logic code to prevent the get request. 所以我添加一些逻辑代码来阻止get请求。

public function myAction()
{
        if($_SERVER['REQUEST_METHOD'] == "GET")
        {
            echo "No get!";
            die();
        }
        else
        {
          //some other codes
        }
}

It works. 有用。 But I have to write these code snippet to many action method. 但我必须将这些代码片段写入许多动作方法。 It looks so redundant. 它看起来很冗余。

So, is there a better way to implement it like the above code? 那么,有没有比上面的代码更好的方法来实现它?

Add this method to your controller 将此方法添加到控制器

public function preDispatch(){
    if(!$this->_request->isPost() and in_array($this->_request->getParam('action'), array('action1', 'action2'))){
        exit('only post');
    }
}

Its too late to answer this question now but hope it will help someone else. 现在回答这个问题为时已晚,但希望它会帮助其他人。

If you want to do this for just one controller then your better off using the controllers _init() method as shown below. 如果您只想为一个控制器执行此操作,那么最好使用控制器_init()方法,如下所示。

public function _init() {
    if(!$this->getRequest()->isPost()){
       //The request is not post. Do what you like.
    }
}

If you want the same thing for multiple controllers you can create a frontController plugin like this. 如果你想为多个控制器做同样的事情你可以像这样创建一个frontController插件。

class Application_Plugin_Request extends Zend_Controller_Plugin_Abstract {
    public function preDispatch(Zend_Controller_Request_Abstract $request){
        if(!$request->$isPost()){
                //The request is not post. Do what you like.   
            }
    }
}

Add this method to your Bootstrap class for activate the plugin. 将此方法添加到Bootstrap类以激活插件。

protected function _initPlugins(){
        $front = Zend_Controller_Front::getInstance();
        $front->registerPlugin(new Application_Plugin_Request(), 17); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM