-- cmple <[email protected]> wrote
(on Monday, 27 February 2012, 04:27 PM -0800):
> HHGAG wrote
> > You only need to return a JsonModel object... If you create a new module
> > or not isn't relevant.
> > 
> Sorry I'm a little lost here, can you help with an example ?
> Ideally I'd want it to be in a single module to not over complicate things
> 
> here is what I've got so far:
> 
> class Module implements AutoloaderProvider
> {
>       ...
>     public function init(Manager $moduleManager)
>     {
>       $moduleManager->events()->attach('loadModules.post', array($this,
> 'modulesLoaded'));

First off, what does this callback do? You don't show it below, and I'm
curious what you're tying into. :)

>         $events = StaticEventManager::getInstance();
>         $events->attach('bootstrap', 'bootstrap', array($this,
> 'initializeView'), 100);
>         $events->attach('bootstrap', 'bootstrap', array($this,
> 'initializeJsonView'));
>     }
>     
>     public function initializeJsonView($e) {
>               $app          = $e->getParam('application');
>               $locator      = $app->getLocator();
>               $view         = $locator->get('Zend\View\View');
>               $jsonRendererStrategy = 
> $locator->get('Zend\View\Strategy\JsonStrategy');
>               
>               $view->events()->attachAggregate($jsonRendererStrategy, 10); 
>     }
>     
>     public function initializeView($e)
>     {
>               $app = $e->getParam('application');             
>               $basePath = $app->getRequest()->getBasePath();
>               $locator = $app->getLocator();
>               $renderer = $locator->get('Zend\View\Renderer\PhpRenderer');
>               $renderer->plugin('url')->setRouter($app->getRouter());
>               $renderer->doctype()->setDoctype('HTML5');
>               $renderer->plugin('basePath')->setBasePath($basePath);
>     }
> ...
> 
> The part that I don't get is how the JsonStragegy activates on the
> front end, I'd like the same page produce a different view based on
> the parameter:
> http://localhost/user/login*?format=json*
> or
> http://localhost/user/login*.json*

The JsonStrategy currently in master utilizes the Accept header to
determine whether or not it gets selected. If the Accept header includes
any media type beginning with "application/json", it will be selected.

The scenario you describe would require either (a) writing a separate
strategy class with listeners for analyzing the request and injecting
the response, or (b) attaching in-place callbacks for the strategy. As
an example of the second, you could actually do these in your
initializeView() method:

    public function initializeView($e)
    {
        $app     = $e->getParam('application');         
        $locator = $app->getLocator();

        $matches      = $e->getRouteMatch();
        $view         = $locator->get('Zend\View\View');
        $jsonRenderer = $locator->get('Zend\View\Renderer\JsonRenderer');

        // Rendering strategy: select JsonRenderer if request indicates
        // we need it
        $view->addRendererStrategy(function ($e) use ($routeMatch, 
$jsonRenderer) {
            $request = $e->getRequest();
            $query   = $request->query();

            if ($query->has('format')) {
                $format = $query->get('format', '');
                if (strtolower($format) == 'json') {
                    // match
                    return $jsonRenderer;
                }
            }

            // Assuming you might capture a ".json" extension as
            // "format":
            $format = $matches->getParam('format', ''); 
            if (strtolower($format) == 'json') {
                // match
                return $jsonRenderer;
            }

            // no match
        }, 10);

        $view->addResponseStrategy(function ($e) use ($jsonRenderer) {
            $renderer = $e->getRenderer();
            if ($renderer !== $jsonRenderer) {
                // Not using the JSON renderer
                return;
            }

            // JsonRenderer was used
            $response = $e->getResponse();
            $result   = $e->getResult();

            // Set Content-Type header, and populate response body
            $response->headers()->addHeaderLine('content-type', 
'application/json');
            $response->setContent($result);
        }, 10);

        // other stuff you were doing would go below...
    }

    

-- 
Matthew Weier O'Phinney
Project Lead            | [email protected]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

-- 
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to