-- Arnaud Limbourg <[EMAIL PROTECTED]> wrote
(on Monday, 26 March 2007, 07:04 AM +0200):
> Matthew Weier O'Phinney wrote:
> > I throw a Zend_View object in the registry, and then access this from my
> > controllers and plugins. The benefit of doing this is that the
> > controllers can set values in the view that are unused in their
> > individual view, but used later in the sitewide template.
> >
> > Then, I use a dispatchLoopShutdown() plugin to inject any generated
> > content into a sitwide template:
> >
> >
> >    class SiteTemplatePlugin extends Zend_Controller_Plugin_Abstract
> >    {
> >        public function dispatchLoopShutdown()
> >        {
> >            $response = Zend_Controller_Front:;getInstance()->getResponse();
> >            $view = Zend_Registry::get('view');
> >            $view->content = $response->getBody();
> >            $response->setBody($view->render('site.phtml'));
> >        }
> >    }
> 
> Which poses a problem when you want to send back json (or whatever) and 
> you don't want a site wide template :)

This was a simple example. But it's actually really easy to return JSON:

    public function dispatchLoopShutdown()
    {
        // assume that we've already determined the request is ajax
        $request  = $this->getRequest();
        $response = $this->getResponse();
        $view     = Zend_Registry::get('view');

        if ($request->getParam('isAjax', false)) {
            // Ajax request detected
            // Get any variables set in the view
            $vars = get_object_vars($view);

            // Merge with named path segments in response
            $vars = array_merge($vars, $response->getBody(true));

            // Create a header and set the response body to a JSON value
            $resposne->setHeader('Content-Type', 'text/x-json');
            $response->setBody(Zend_Json::encode($vars));
            return;
        }

        // Otherwise, process as normal
        $view->content = $response->getBody();
        $response->setBody($view->render('site.phtml'));
    }

-- 
Matthew Weier O'Phinney
PHP Developer            | [EMAIL PROTECTED]
Zend - The PHP Company   | http://www.zend.com/

Reply via email to