2

が付いたZendFrameworkコントローラーがありeditAction()ます。

class WidgetController extends BaseController
{
   public function editAction()
   {
       //code here
   }
}

このコントローラーは、ユーザーがレコードを編集できるようにする前に、ユーザーがログインしているかどうかを確認するベースコントローラーを拡張します。

class BaseController extends Zend_Controller_Action
{
   public function init()
   {
       if ($this->userNotLoggedIn()) {
           return $this->_redirect('/auth/login');
       }
   }
}

ただし、AJAXリクエストを実行しているので、JSONレスポンスを送り返すため、リダイレクトは機能しなくなります。すぐに応答を送信できるように、コントローラーの実行を停止する必要があります。

class BaseController extends Zend_Controller_Action
{
   public function init()
   {
       if ($this->userNotLoggedIn()) {
           if ($this->_request->isXmlHttpRequest()) {
               $jsonData = Zend_Json::encode(array('error'=>'You are not logged in!'));
               $this->getResponse()
                    ->setHttpResponseCode(401)
                    ->setBody($jsonData)
                    ->setHeader('Content-Type', 'text/json');
               //now stop controller execution so that the WidgetController does not continue
           } else {
               return $this->_redirect('/auth/login');
           }
       }
   }
}

コントローラの実行を停止するにはどうすればよいですか?

4

2 に答える 2

5

ログインしていないユーザーを定義し、XMLHTTPRequestを例外状態にしようとすると、エラーハンドラーが例外をスローして処理できるようにします(これにより、現在のアクションのディスパッチが停止します)。そうすれば、発生する可能性のある他の種類の例外を処理することもできます。

class BaseController extends Zend_Controller_Action
{
   public function init()
   {
       if ($this->userNotLoggedIn()) {
           if ($this->_request->isXmlHttpRequest()) {
                throw new Exception('You are not logged in', 401);
           } else {
               return $this->_redirect('/auth/login');
           }
       }
   }
}

class ErrorController extends Zend_Controller_Action
{

    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');
        $exception = $errors->exception;

       if ($this->_request->isXmlHttpRequest()) {
           $jsonData = Zend_Json::encode($exception);
            $jsonData = Zend_Json::encode(array('error'=> $exception->getMessage()));
            $isHttpError = $exception->getCode() > 400 && $exception->getCode();
            $code =  $isHttpError ? $exception->getCode() : 500;
           $this->getResponse()
                ->setHttpResponseCode($code)
                ->setBody($jsonData)
                ->setHeader('Content-Type', 'application/json');
        } else {
            // Render error view
        }
    }
}
于 2011-07-22T18:41:15.087 に答える
5

コードのこの時点でコントローラーを停止する方法はたくさん考えられます。

//now stop controller execution so that the WidgetController does not continue

1つは、その行を次のように置き換えることができます。

$this->getResponse()->sendResponse();
exit;

それは最もクリーンではないかもしれませんが、仕事をかなりうまくやり遂げます。もう1つのオプションは、内のリクエストのアクションを変更し、init別のアクションに処理させることです。その行を次のように置き換えます。

 $this->getRequest()->setActionName('invalid-user');

すでにディスパッチャ内にあるため、必要かどうかに関係なく、アクションクラス内でアクションを実行します。preDispatchでリクエストを変更しようとしても、このディスパッチは変更されません。この時点で、クラス内でアクションを実行することが決定されています。だから、それを処理するためのアクションを作成します。

public function invalidUserAction()
{
    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
}

詳細については、Zend_Controller_Dispatcher_Standard::dispatchを参照してください。

于 2011-07-22T19:43:17.570 に答える