Search code examples
ajaxzend-frameworkzend-controller

Zend Framework: How to stop dispatch/controller execution?


I have a Zend Framework controller with an editAction().

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

This controller extends a base controller which checks if the user is logged in before allowing the user to edit a record.

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

However, now that I am performing an AJAX request, I will be sending a JSON response back, so a redirect will no longer work. I need to stop further controller execution so I can immediately send a response:

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');
           }
       }
   }
}

How can I stop controller execution?


Solution

  • I would define the user not being logged in and trying to make an XMLHTTPRequest as an exceptional state and let the error handler deal with it by throwing an exception (which stops dispatching of the current action). That way you are also able to handle other kinds of exceptions that might happen:

    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
            }
        }
    }