|
Hi,
I'm creating an app, which outputs json but I'm not very successful with it. In my module.php I have tried to register JsonStrategy, but the examples I have found seems to have old code... Does anyone have updated example on this subject? I'm using the latest RC. br, Marko
br,
-------------------------- Marko Korhonen Technical Consultant |
|
2012/8/10 Marko78 <[hidden email]>
> Hi, > > I'm creating an app, which outputs json but I'm not very successful with > it. > > In my module.php I have tried to register JsonStrategy, but the examples > I have found seems to have old code... > > Does anyone have updated example on this subject? > I'm using the latest RC. > > br, Marko > Recently I tried this out too. I have blogged about my findings, which you can read here: http://juriansluiman.nl/en/article/119/attach-a-view-strategy-for-specific-modules Hope that helps. -- Jurian Sluiman |
|
Thanks, it helped one step further. =)
Now is still tries to render stuff with PhpRenderer: 'Zend\View\Renderer\PhpRenderer::render: Unable to render template "application/user/index"' br, Marko
br,
-------------------------- Marko Korhonen Technical Consultant |
|
hmm, when i test the controller with curl:
curl -H 'Accept: application/json' http://mydomain/user/1 I get following: {"content":{"content":"Placeholder page"}} Where is this coming from? br, Marko
br,
-------------------------- Marko Korhonen Technical Consultant |
|
2012/8/10 Marko78 <[hidden email]>
> hmm, when i test the controller with curl: > > curl -H 'Accept: application/json' http://mydomain/user/1 > > I get following: > > {"content":{"content":"Placeholder page"}} > > Where is this coming from? > > br, Marko > Marko, This is the the intended behaviour from the JSON strategy. You should read the manual about alternative rendering strategies. Check this link under the section "Creating and Registering Alternate Rendering and Response Strategies": http://zf2.readthedocs.org/en/latest/modules/zend.view.quick-start.html The JSON strategy basically returns JSON when you 1. Return a Zend\View\Model\ViewModel in your controller and have a request which contains an accept header with application/json 2. Explicitly return a Zend\View\Model\JsonModel If you want to return JSON without looking at the accept http header, you should return a JsonModel instead of a ViewModel. -- Jurian Sluiman |
|
Hi,
is it possible to make whole application output json? so i would not need to do this in every module. br, Marko
br,
-------------------------- Marko Korhonen Technical Consultant |
|
2012/8/10 Marko78 <[hidden email]>
> Hi, > > is it possible to make whole application output json? > so i would not need to do this in every module. > > br, Marko > Rob Allen has also an excellent article: http://akrabat.com/zend-framework-2/returning-json-from-a-zf2-controller-action/ -- Jurian Sluiman |
|
Cool,
with that link and with this: http://jacekonthings.blogspot.fi/2012/05/how-to-create-restful-api-in-zend.html I was able to get it working =) br, Marko
br,
-------------------------- Marko Korhonen Technical Consultant |
|
Administrator
|
In reply to this post by Marko78
-- Marko78 <[hidden email]> wrote
(on Friday, 10 August 2012, 01:45 AM -0700): > is it possible to make whole application output json? > so i would not need to do this in every module. JSON all the time, or under certain conditions? You can definitely register the JsonStrategy at the application level so that it can be utilized by any module -- but the rules as noted in previous emails in this thread still apply: it will only trigger if the Accept header indicates an application/json content-type is allowed, or if you return a JsonModel from your actions. If you want JSON _all_ the time -- as in, no HTML, ever, you would need to create a view listener that returns the JsonRenderer in all cases -- look at the PhpRendererStrategy as an example. -- Matthew Weier O'Phinney Project Lead | [hidden email] Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc -- List: [hidden email] Info: http://framework.zend.com/archives Unsubscribe: [hidden email] |
| Powered by Nabble | Edit this page |
