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 8694335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:45:48+00:00 2026-06-13T00:45:48+00:00

In Zend Framework 2, content negotiation happens on the view layer and I am

  • 0

In Zend Framework 2, content negotiation happens on the view layer and I am pretty happy with it. An example in my controller:

public function viewAction()
{
    $id   = $this->params('id');
    $user = $this->getRepository()->findUser();

    return new ViewModel(array(
        'user' => $user,
    ));
}

This either renders the view.phtml template to return html or it converts the user object to a JSON response. A so-called view strategy determines how to render the response based on the request.

“REST” Application Flow in my webapp

This type of content negotiation works pretty good for many use cases:

  1. /user or indexAction() returns an array of users => html of JSON possible;
  2. /user/1 or viewAction() returns user object => html or JSON possible (example from above);
  3. /user/1/update or updateAction() returns a html form. A POST to this url returns html or JSON when errors are present. Or on success it redirects to viewAction() and thus returns the new version of the user => html and JSON again possible;
  4. /user/create or createAction() returns a html form. A POST to this url returns html or JSON when errors are present. Or on success it redirects to viewAction() for the just created user => html and JSON again possible

My Question

There are a few use cases where the content negotiation is sort-of “required” in the controller layer. I am not sure if I overlook some possibilities: are there options I can use in for example the following cases?

  1. Delete a user: POST to the /user/1/delete. In case of a html view, you will be redirected to the list of users (where the deleted user is now missing). In case you want a JSON response, you want to return 200 OK and a JSON object with a message the delete was successful.
  2. Post a comment to a blog article. In case of a html view, you will be redirected to the post where you see your comment is appended. In case you ask for a JSON response, you want to return 200 OK and a JSON object with the comment you just placed.

My goal would be to not replicate the content negotiation already present in the view layer. It would also make my controllers more fat, since I have now two possible responses (JSON vs html) but that might not be the only case. If I later want to support XML or another format, I have for every action switches for those response types.

  • 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-06-13T00:45:49+00:00Added an answer on June 13, 2026 at 12:45 am

    Interestingly, we’re looking currently at moving the content negotiation aspect out of the view strategy listeners, and instead into controller plugins. The rationale is largely as you note — it’s the controller’s job to match an incoming request to the appropriate model(s) and view. As such, yes, I think you’re on the right track — and likely the tools being developed now for 2.1 will fit the methodologies you have quite nicely. (See https://github.com/zendframework/zf2/pull/2615 for details.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm following this example tutorial project code: http://akrabat.com/wp-content/uploads/zf-tutorial-layoutform.zip tutorial: http://akrabat.com/zend-framework/a-form-in-your-layout/ The project code runs
I have a zend framework layout like this: ... <div id=div1></div> <?php echo $this->layout()->content;
I am using context switching to get json response in Zend framework This is
I am learning AJAX in zend framework step by step. I use this question
Note: I'm using Zend Framework, but I think most of this applies to PHP
I am developing an application using Zend Framework 1.11.10. I have used this article
I am using Zend Framework with PHPTAL templates for view sites. Problem is that
I've already seen this question: Zend Framework how to set headers and I am
I have a Zend Framework controller with an editAction() . class WidgetController extends BaseController
Trying to setup Zend Framework as instructed in this manual $paths = array( realpath(dirname(__FILE__)

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.