Quantcast

View Layer ready to test!

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

View Layer ready to test!

weierophinney
Administrator
Greets, all!

I've had a number of discussions and ideas about the view layer over the
past week, and got rather enthused and on a roll with coding. As a
result, the view layer is ready to test!

The easiest way to test is to grab my feature/view-layer branch of the
ZendSkeletonApplication:

    git clone -b feature/view-layer --recursive https://github.com/weierophinney/ZendSkeletonApplication

This will get you the skeleton application, but refactored to use the
new view layer from my feature/view-layer branch of my ZF2 fork.

The pieces to look at primarily are module/Application/Module.php and
module/Application/config/module.config.php. In the case of the latter,
the "view" integration right now is simply seeding some common view
helpers (a problem I'm still working on). In the case of the former,
you'll see a number of new DI configurations for a variety of "view
strategies" -- exception/errors, route not found, and the default
rendering strategy. These are used to set error, 404, and layout
templates.

If you look at the controllers, you'll also see that they've been
modified to return ViewModels; I may still add functionality to support
returning arrays, but I'm on the fence on this one. Regardless, there is
a strategy in place to auto-inject the view model's template based on
the controller in the route match (and optionally the action); this
strategy simply strips the namespace from the matched controller and
normalizes it to lowercase, dash-separated words. If you specify a
template in the ViewModel (using the setTemplate() method), that
template will be used, and no injection will occur.

The MVC Event now composes a ViewModel. This model acts as the "root"
model -- typically the layout. If you want to change the layout, grab
the view model and change its template:

    $e->getViewModel()->setTemplate('alternate/layout');

The result of an MVC action, if a ViewModel, is injected as a child of
this ViewModel (by the InjectViewModelStrategy).

I also have additional strategies in the library, but not enabled by
default, for rendering JSON and Atom/RSS feeds.

At this point, everything "just works". I'll write up more on it in the
next day or two as I finalize the remaining tasks, but please start
testing and playing with it!

--
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: View Layer ready to test!

EvanDotPro
On Sun, Feb 12, 2012 at 6:41 PM, Matthew Weier O'Phinney
<[hidden email]> wrote:

> Greets, all!
>
> I've had a number of discussions and ideas about the view layer over the
> past week, and got rather enthused and on a roll with coding. As a
> result, the view layer is ready to test!
>
> The easiest way to test is to grab my feature/view-layer branch of the
> ZendSkeletonApplication:
>
>    git clone -b feature/view-layer --recursive https://github.com/weierophinney/ZendSkeletonApplication
>

Looks like the remote for the ZF2 submodule is still pointed to the
zendframework/zf2 repo whereas it should be changed to point to your
fork since you have the submodule pointed to a commit that only exists
on your fork. Right now initializing the submodule yields something
like this:

fatal: reference is not a tree: c85c45a427afd03f5efb6c6fe31220ae97bb1175
Unable to checkout 'c85c45a427afd03f5efb6c6fe31220ae97bb1175' in
submodule path 'vendor/ZendFramework'

--
Evan Coury
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: View Layer ready to test!

weierophinney
Administrator
-- Evan Coury <[hidden email]> wrote
(on Sunday, 12 February 2012, 08:55 PM -0700):

> On Sun, Feb 12, 2012 at 6:41 PM, Matthew Weier O'Phinney
> <[hidden email]> wrote:
> > Greets, all!
> >
> > I've had a number of discussions and ideas about the view layer over the
> > past week, and got rather enthused and on a roll with coding. As a
> > result, the view layer is ready to test!
> >
> > The easiest way to test is to grab my feature/view-layer branch of the
> > ZendSkeletonApplication:
> >
> >    git clone -b feature/view-layer --recursive https://github.com/weierophinney/ZendSkeletonApplication
> >
>
> Looks like the remote for the ZF2 submodule is still pointed to the
> zendframework/zf2 repo whereas it should be changed to point to your
> fork since you have the submodule pointed to a commit that only exists
> on your fork. Right now initializing the submodule yields something
> like this:

Fixed -- thanks for the heads-up!


--
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: View Layer ready to test!

BullfrogBlues
This post has NOT been accepted by the mailing list yet.
In reply to this post by weierophinney

I can't get past "Model". Seriously, I have netbeans open in front of me, and I'm stuck on this:

return new ViewModel();
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: View Layer ready to test!

Adam Lundrigan
In reply to this post by weierophinney
Not sure if this is still a WIP, but how does one wire up the skeleton application so that when a JsonModel is returned from a controller it outputs JSON?  When I return a JsonModel, the behavior is exactly the same as if I had returned a ViewModel  (ie: layout + template rendered by PhpRenderer).  I'm using the HEAD of the feature/view-layer branch.

Thanks,

--
Adam Lundrigan, B.Sc, ZCE
[hidden email]


On Mon, Feb 13, 2012 at 9:26 AM, Matthew Weier O'Phinney <[hidden email]> wrote:
-- Evan Coury <[hidden email]> wrote
(on Sunday, 12 February 2012, 08:55 PM -0700):
> On Sun, Feb 12, 2012 at 6:41 PM, Matthew Weier O'Phinney
> <[hidden email]> wrote:
> > Greets, all!
> >
> > I've had a number of discussions and ideas about the view layer over the
> > past week, and got rather enthused and on a roll with coding. As a
> > result, the view layer is ready to test!
> >
> > The easiest way to test is to grab my feature/view-layer branch of the
> > ZendSkeletonApplication:
> >
> >    git clone -b feature/view-layer --recursive https://github.com/weierophinney/ZendSkeletonApplication
> >
>
> Looks like the remote for the ZF2 submodule is still pointed to the
> zendframework/zf2 repo whereas it should be changed to point to your
> fork since you have the submodule pointed to a commit that only exists
> on your fork. Right now initializing the submodule yields something
> like this:

Fixed -- thanks for the heads-up!


--
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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: View Layer ready to test!

weierophinney
Administrator
-- Adam Lundrigan <[hidden email]> wrote
(on Wednesday, 15 February 2012, 07:26 PM -0330):
> Not sure if this is still a WIP, but how does one wire up the skeleton
> application so that when a JsonModel is returned from a controller it outputs
> JSON?  When I return a JsonModel, the behavior is exactly the same as if I had
> returned a ViewModel  (ie: layout + template rendered by PhpRenderer).  I'm
> using the HEAD of the feature/view-layer branch.

This is something I've been meaning to document. :)

In either your Application module, or a module that might use JSON,
register a bootstrap listener that does something like the following:

    public function wireJsonStrategy($e)
    {
        $app          = $e->getParam('application');
        $locator      = $app->getLocator();
        $view         = $locator->get('Zend\View\View');
        $jsonStrategy = $locator->get('Zend\View\Strategy\JsonStrategy');

        // Attach rendering and response strategies to view
        $view->addRenderingStrategy(array($jsonStrategy, 'selectRenderer'), 10);
        $view->addResponseStrategy(array($jsonStrategy,  'injectResponse'), 10);
    }

You could alternately attach this to the "route" event as a low priority
listener, and query the RouteMatch to see if the controller matched is
in this particular module, attaching the strategies only then.


> On Mon, Feb 13, 2012 at 9:26 AM, Matthew Weier O'Phinney <[hidden email]>
> wrote:
>
>     -- Evan Coury <[hidden email]> wrote
>     (on Sunday, 12 February 2012, 08:55 PM -0700):
>     > On Sun, Feb 12, 2012 at 6:41 PM, Matthew Weier O'Phinney
>     > <[hidden email]> wrote:
>     > > Greets, all!
>     > >
>     > > I've had a number of discussions and ideas about the view layer over
>     the
>     > > past week, and got rather enthused and on a roll with coding. As a
>     > > result, the view layer is ready to test!
>     > >
>     > > The easiest way to test is to grab my feature/view-layer branch of the
>     > > ZendSkeletonApplication:
>     > >
>     > >    git clone -b feature/view-layer --recursive https://github.com/
>     weierophinney/ZendSkeletonApplication
>     > >
>     >
>     > Looks like the remote for the ZF2 submodule is still pointed to the
>     > zendframework/zf2 repo whereas it should be changed to point to your
>     > fork since you have the submodule pointed to a commit that only exists
>     > on your fork. Right now initializing the submodule yields something
>     > like this:
>
>     Fixed -- thanks for the heads-up!
>
>
>     --
>     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
>
>

--
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: View Layer ready to test!

Adam Lundrigan
Matthew,

Thanks for the tip!  I was close...my last attempt was modeled on how PhpRendererStrategy is set up in Zend\Mvc\Bootstrap::setupView:

        $view                = $locator->get('Zend\View\View');
        $jsonRendererStrategy = $locator->get('Zend\View\Strategy\JsonStrategy');
        $view->events()->attachAggregate($jsonRendererStrategy);

--
Adam Lundrigan, B.Sc, ZCE
[hidden email]


On Thu, Feb 16, 2012 at 1:19 PM, Matthew Weier O'Phinney <[hidden email]> wrote:
-- Adam Lundrigan <[hidden email]> wrote
(on Wednesday, 15 February 2012, 07:26 PM -0330):
> Not sure if this is still a WIP, but how does one wire up the skeleton
> application so that when a JsonModel is returned from a controller it outputs
> JSON?  When I return a JsonModel, the behavior is exactly the same as if I had
> returned a ViewModel  (ie: layout + template rendered by PhpRenderer).  I'm
> using the HEAD of the feature/view-layer branch.

This is something I've been meaning to document. :)

In either your Application module, or a module that might use JSON,
register a bootstrap listener that does something like the following:

   public function wireJsonStrategy($e)
   {
       $app          = $e->getParam('application');
       $locator      = $app->getLocator();
       $view         = $locator->get('Zend\View\View');
       $jsonStrategy = $locator->get('Zend\View\Strategy\JsonStrategy');

       // Attach rendering and response strategies to view
       $view->addRenderingStrategy(array($jsonStrategy, 'selectRenderer'), 10);
       $view->addResponseStrategy(array($jsonStrategy,  'injectResponse'), 10);
   }

You could alternately attach this to the "route" event as a low priority
listener, and query the RouteMatch to see if the controller matched is
in this particular module, attaching the strategies only then.


> On Mon, Feb 13, 2012 at 9:26 AM, Matthew Weier O'Phinney <[hidden email]>
> wrote:
>
>     -- Evan Coury <[hidden email]> wrote
>     (on Sunday, 12 February 2012, 08:55 PM -0700):
>     > On Sun, Feb 12, 2012 at 6:41 PM, Matthew Weier O'Phinney
>     > <[hidden email]> wrote:
>     > > Greets, all!
>     > >
>     > > I've had a number of discussions and ideas about the view layer over
>     the
>     > > past week, and got rather enthused and on a roll with coding. As a
>     > > result, the view layer is ready to test!
>     > >
>     > > The easiest way to test is to grab my feature/view-layer branch of the
>     > > ZendSkeletonApplication:
>     > >
>     > >    git clone -b feature/view-layer --recursive https://github.com/
>     weierophinney/ZendSkeletonApplication
>     > >
>     >
>     > Looks like the remote for the ZF2 submodule is still pointed to the
>     > zendframework/zf2 repo whereas it should be changed to point to your
>     > fork since you have the submodule pointed to a commit that only exists
>     > on your fork. Right now initializing the submodule yields something
>     > like this:
>
>     Fixed -- thanks for the heads-up!
>
>
>     --
>     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
>
>

--
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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: View Layer ready to test!

weierophinney
Administrator
-- Adam Lundrigan <[hidden email]> wrote
(on Thursday, 16 February 2012, 08:05 PM -0330):
> Thanks for the tip!  I was close...my last attempt was modeled on how
> PhpRendererStrategy is set up in Zend\Mvc\Bootstrap::setupView:
>
>         $view                = $locator->get('Zend\View\View');
>         $jsonRendererStrategy = $locator->get('Zend\View\Strategy\
> JsonStrategy');
>         $view->events()->attachAggregate($jsonRendererStrategy);

One thing I need to change here is that right now, when you use
attachAggregate, no priority value can be assigned other than what's in
your ListenerAggregate's attach() method. The problem with this is that,
by default, these are registering at the same priority -- which means
that the first to register wins. Since that's the PhpRendererStrategy right now,
and because it returns the PhpRenderer unconditionally, that basically
trumps any others -- unless you register them manually with a higher
priority:

    $view->addRendererStrategy(array($jsonRendererStrategy, 'selectRenderer'), 10);
    $view->addResponseStrategy(array($jsonRendererStrategy, 'selectResponse'), 10);

What I'd like to do is something like this:

    $view->events()->attachAggregate($jsonRendererStrategy, 10);

and use that priority value for all listeners attached, if it's present.


> On Thu, Feb 16, 2012 at 1:19 PM, Matthew Weier O'Phinney <[hidden email]>
> wrote:
>
>     -- Adam Lundrigan <[hidden email]> wrote
>     (on Wednesday, 15 February 2012, 07:26 PM -0330):
>     > Not sure if this is still a WIP, but how does one wire up the skeleton
>     > application so that when a JsonModel is returned from a controller it
>     outputs
>     > JSON?  When I return a JsonModel, the behavior is exactly the same as if
>     I had
>     > returned a ViewModel  (ie: layout + template rendered by PhpRenderer).
>      I'm
>     > using the HEAD of the feature/view-layer branch.
>
>     This is something I've been meaning to document. :)
>
>     In either your Application module, or a module that might use JSON,
>     register a bootstrap listener that does something like the following:
>
>        public function wireJsonStrategy($e)
>        {
>            $app          = $e->getParam('application');
>            $locator      = $app->getLocator();
>            $view         = $locator->get('Zend\View\View');
>            $jsonStrategy = $locator->get('Zend\View\Strategy\JsonStrategy');
>
>            // Attach rendering and response strategies to view
>            $view->addRenderingStrategy(array($jsonStrategy, 'selectRenderer'),
>     10);
>            $view->addResponseStrategy(array($jsonStrategy,  'injectResponse'),
>     10);
>        }
>
>     You could alternately attach this to the "route" event as a low priority
>     listener, and query the RouteMatch to see if the controller matched is
>     in this particular module, attaching the strategies only then.
>
>
>     > On Mon, Feb 13, 2012 at 9:26 AM, Matthew Weier O'Phinney <
>     [hidden email]>
>     > wrote:
>     >
>     >     -- Evan Coury <[hidden email]> wrote
>     >     (on Sunday, 12 February 2012, 08:55 PM -0700):
>     >     > On Sun, Feb 12, 2012 at 6:41 PM, Matthew Weier O'Phinney
>     >     > <[hidden email]> wrote:
>     >     > > Greets, all!
>     >     > >
>     >     > > I've had a number of discussions and ideas about the view layer
>     over
>     >     the
>     >     > > past week, and got rather enthused and on a roll with coding. As
>     a
>     >     > > result, the view layer is ready to test!
>     >     > >
>     >     > > The easiest way to test is to grab my feature/view-layer branch
>     of the
>     >     > > ZendSkeletonApplication:
>     >     > >
>     >     > >    git clone -b feature/view-layer --recursive https://github.com
>     /
>     >     weierophinney/ZendSkeletonApplication
>     >     > >
>     >     >
>     >     > Looks like the remote for the ZF2 submodule is still pointed to the
>     >     > zendframework/zf2 repo whereas it should be changed to point to
>     your
>     >     > fork since you have the submodule pointed to a commit that only
>     exists
>     >     > on your fork. Right now initializing the submodule yields something
>     >     > like this:
>     >
>     >     Fixed -- thanks for the heads-up!
>     >
>     >
>     >     --
>     >     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
>     >
>     >
>
>     --
>     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
>
>

--
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: View Layer ready to test!

macest
This post has NOT been accepted by the mailing list yet.
I'm trying to figure out how to use a view script to output JSON. From what I can tell you don't seem to be able to do this with the JSON renderer, it simply serializes the model variables and outputs the result. Any thoughts on this?
Loading...