Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6164923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:04:35+00:00 2026-05-23T22:04:35+00:00

I have a Zend Framework controller with an editAction() . class WidgetController extends BaseController

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:04:36+00:00Added an answer on May 23, 2026 at 10:04 pm

    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
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a Zend Framework question. If I have a Controller, an Action Helper,
So I'm using zend framework and I have this code in a file called
I am using the Zend Framework. I have a controller named 'UserController' that has
Zend framework: what is the significance of controller filenames that have init in them?
As mentioned in the Zend Framework manual , I created a base controller. Subclassing
i am getting started with zend framework.I have encountered this problem with forms. when
Both zend and CI framework have seperate Model, View, Controller directories. My doubt is
i am developing a Rest Controller using Zend Framework and i have to use
i have been using a modular structure for my zend framework application. This is
Server side extjs form validation using zend framework I have extended Zend_Form class to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.