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

  • SEARCH
  • Home
  • 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 6585449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:39:54+00:00 2026-05-25T16:39:54+00:00

I need a controller in my Zend Framework project, which should handle only ajax

  • 0

I need a controller in my Zend Framework project, which should handle only ajax requests.

My approach at the moment is to extend the Zend_Controller_Action:

class Ht_Ajax_Controller extends Zend_Controller_Action{
    public function preDispatch(){
        $this->getResponse()->setHeader('Content-type', 'text/plain')
                            ->setHeader('Cache-Control','no-cache');
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout()->disableLayout();
    }

    public function outputJson($data){
        $this->getResponse()->setBody(json_encode($data))
                            ->sendResponse();
        exit;
    }
}

Although, I know this isn’t the recommended way to do so in Zend Framework. So that’s why I am asking, how to make this in Zend’s way ?

The first thing I thought about was to make a controller plugin, but how can I register this plugin easily? The bootstrap is no option because I need this only for specific controllers. And to register it inside the controller in a very early state seems not very clean.

So how should I implement my Ajax Controller? I also know there is the extended Context switching helper ,however I think that way has too much overhead for just setting the content type etc.

  • 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-25T16:39:55+00:00Added an answer on May 25, 2026 at 4:39 pm

    markus explained the most “Zend Like” way and his explanation is correct. But you see it as too much overhead because he missed to show you how much the “json context switch” is the best for your case.

    Look how short it is :

    //In your controller, activate json context for ANY called action in once
    
    public function init(){
        $action = $this->_getParam('action');
        $this->_helper->getHelper('contextSwitch')
             ->addActionContext($action, 'json')
             ->initContext();
    }
    

    And thats all, you don’t need any view scripts, all the variables you assign via $this->view will be serialized into a JSON object.

    Oh and you don’t want to add the context in the urls? Ok, just activate it by default in your route

    routes.ajax.route = '/ajax/:action/*'
    routes.ajax.defaults.controller = ajax
    routes.ajax.defaults.action = index
    routes.ajax.defaults.format = json
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a need for chained select boxes in my zend framework project: Countries->Regions->Provinces->Towns.
I am learning AJAX in zend framework step by step. I use this question
In Zend Framework, I have a common use case where I need to propagate
Avoiding Fat Controller So I'm using Zend Framework and I have a question involving
The standard extension for template files in Zend Framework is .phtml... I need to
I'm writing tests for my current project, made with Zend Framework. Everything's fine, but
As mentioned in the Zend Framework manual , I created a base controller. Subclassing
I need a little advice how to make Zend Framework breadcrumbs more complex than
How run zend framework action (inside index controller) by cron every 12 hours? The
I am working on implementing Zend Framework within an existing project that has a

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.