Zend Framework 2: Change Action Response Based on Request Format?

I’m sure this is possible in zf2 though I’m not all that familiar. Any I’m trying to replicate rails formats where if you make a request like:


The create method of the user controller is invoked with a default response of HTML.

However, this request:


Would return a json response for the state of the action.

Could someone point me to the ZF2 documentation for doing that?

Rails snippet:

  def create
   @classified = Classified.new

    respond_to do |format|
      if @classified.save
        format.html { redirect_to @classified, notice: 'Classified was successfully created.' }
        format.json { render json: @classified, status: :created, location: @classified }
        format.html { render action: "new" }
        format.json { render json: @classified.errors, status: :unprocessable_entity }


Surprise, surprise I don’t think ZF2 is capable of this. At least not as easy as it would be in rails or Symfony 2. shrug…

Looks like the *best method would be to check for some type of flag manually than change the rendering strategy.

Could they have made it any more of a clusterf**k… I think not.

Ya, I spent about an hour going through Zend 2 before quitting. I’ll stick with 1.x, not that it’s much better, but Zend 2 didn’t look like something I wanted to pick up. Seems like more code to do less.

I have no choice. Some other developers have chosen to use ZF2 for a project. We are going to use that project as a starting point for an up and coming one. At least that is the plan. I have already stated my concerns and recommend using Symfony 2, which should have been used in the first place due to our marriage with Drupal. At least by using Symfony 2 we could get the hell away from Drupal while also increasing our knowledge in it for future versions since Drupal 8+ is transitioning into Symfony 2 or a significant portion of it. We will see. I’m really just happy to work with something other Drupoo but I prefer it to be Symfony 2…