Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Zend _forward()在preDispatch()中不起作用?_Php_Zend Framework_Execution Time - Fatal编程技术网

Php Zend _forward()在preDispatch()中不起作用?

Php Zend _forward()在preDispatch()中不起作用?,php,zend-framework,execution-time,Php,Zend Framework,Execution Time,我目前正在从我的Zend MVC应用程序构建一个控制器,该应用程序将仅用作json服务来填充页面。我想限制用户仅使用GET方法访问此端点(出于某些安全原因) 我跟随了这个职位,但没有找到工作 我正在使用preDispatch检测非get请求,并希望转发到同一控制器中的errorAction。我的代码看起来像这样 public function preDispatch(){ $this->_helper->layout()->disableLayout(); $t

我目前正在从我的Zend MVC应用程序构建一个控制器,该应用程序将仅用作json服务来填充页面。我想限制用户仅使用GET方法访问此端点(出于某些安全原因)

我跟随了这个职位,但没有找到工作

我正在使用preDispatch检测非get请求,并希望转发到同一控制器中的errorAction。我的代码看起来像这样

public function preDispatch(){
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
    //Restrict this Controller access to Http GET method
    if(!($this->getRequest()->isGet())){
        return $this->_forward('error');
    }
}

public function errorAction(){
    $this->getResponse()->setHttpResponseCode(501);
    echo "Requested Method is not Implemented";
}
当我用post请求测试页面时,它抛出

PHP致命错误:超过最大执行时间30秒

我和它一起工作

$this->_redirect("service/error");
想知道这是否是处理这种情况的唯一/最佳方法


任何帮助都将不胜感激。提前感谢。

调用
\u forward
不起作用的原因是请求方法没有改变,因此您最终进入了一个无限循环,试图转发到
错误操作,因为请求总是
POST

\u forward
通过修改发送请求时将调用的模块、控制器和操作来工作,
\u redirect
实际上返回302重定向,并导致浏览器发出额外的HTTP请求

这两种方法都可以,但我更喜欢使用
\u forward
,因为它不需要额外的HTTP请求(但您仍然保证
POST
请求被拒绝)

此代码应适用于您:

    if(!($this->getRequest()->isGet())){
        // change the request method - this only changes internally
        $_SERVER['REQUEST_METHOD'] = 'GET';

        // forward the request to the error action - preDispatch is called again
        $this->_forward('error');

        // This is an alternate to using _forward, but is virtually the same
        // You still need to override $_SERVER['REQUEST_METHOD'] to do this
        $this->getRequest()
             ->setActionName('error')
             ->setDispatched(false);
    }

令人惊叹的。。。工作得很有魅力。。!谢谢你的快速回复:D