2012-06-11 2 views
2

현재 페이지를 채우는 데 json 서비스로만 사용되는 Zend MVC 애플리케이션으로 컨트롤러를 구축하고 있습니다. GET 메서드 만 사용하여이 끝점에 액세스하도록 사용자에게 제한하고 싶습니다 (보안상의 이유로).Zend _forward()가 preDispatch()에서 작동하지 않습니까?

나는이 게시물 _forward() in Zend does not work?을 따라 갔지만 작동하지 못했습니다.

나는 preDispatch를 사용하여 요청을 감지하지 못하고 동일한 컨트롤러에서 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 요청으로 페이지를 테스트, 그것이 내가 그것이 유일한 경우 궁금

$this->_redirect("service/error"); 

작업있어

PHP Fatal error: Maximum execution time of 30 seconds exceeded

을 던졌습니다 보이는 /이 상황을 처리하는 가장 좋은 방법.

도움이 될 것입니다. 미리 감사드립니다.

답변

2

_forward을 호출하는 이유는 요청 방법이 변경되지 않기 때문에 요청이 항상 POST이므로 error 작업으로 전달하려고하는 무한 루프로 끝나기 때문입니다.

_forward은 요청이 전달 될 때 호출 할 모듈, 컨트롤러 및 동작을 수정하여 작동하며 실제로는 302 리디렉션을 반환하고 브라우저에서 추가 HTTP 요청을 발생시킵니다.

두 가지 방법 모두 괜찮습니다. 추가 HTTP 요청이 필요하지 않으므로 _forward을 선호합니다 (단, 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); 
    } 
+0

최고는 ... 마법처럼 일했다 ..! 빠른 응답 주셔서 감사합니다 : D 조 –