Using view strategies/renderers
Normally we will use the View to output HTML, but sometimes we want a more diverse way of outputting for example JSON or XML. This recipe will provide us with enough information to accomplish this easily.
Getting ready
We simply require the Zend Framework 2 skeleton application to get us going with this recipe. Nothing exotic is required.
How to do it…
Using different view strategies and renderers is a common practice in an application. In this recipe we'll explain how to do that.
Adding a view strategy
We can easily add a view strategy to our application by simply appending the view_manager
configuration in the module configuration file (/module/Restful/config/module.config.php
) as shown as follows:
<?php return array( 'view_manager' =>array( 'strategies' => array( // This could also be ViewFeedStrategy if we want // to output as a feed 'ViewJsonStrategy', ), ), );
The JSON strategy
If we receive output from the JSON strategy...