Quantcast

Configuring a Module's Controller Classes in ZF2...

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

Configuring a Module's Controller Classes in ZF2...

travis
I noticed in the ZF2 Skeleton application the following Application module configuration section:
    'controller' => array(
        'classes' => array(
            'index' => 'Application\Controller\IndexController'
        ),
    ),

This SEEMS to make "index" an alias of "Application\Controller\IndexController", which is then used in the routes section whenever a controller is specified.  If I'm wrong here, please let me know.

Then, I see Matthew Weier O'Phinney do something similar:
    'controller' => array(
        'class' => array(
            'application-page'                      => 'Application\Controller\PageController',
            'Application\Controller\PageController' => 'Application\Controller\PageController',
        ),
    ),

However, in his routes, he specifies a full controller name: Application\Controller\PageController.

I did it both ways and even commented out my whole controller section and it didn't seem to make any difference.  The only thing that appeared to make my routes use the correct controller was to specify a full controller name like Matthew did.

I also know from another thread here that Matthew has advised against using aliases, which may or may not be applicable here....I'm not sure.

Anyway, I would, again, like some clarification on the use of the controller section, whether or not its necessary, what it's used for, and whether or not aliases should or could be used.

Thanks in advance....
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Configuring a Module's Controller Classes in ZF2...

weierophinney
Administrator
-- travis <[hidden email]> wrote
(on Wednesday, 30 May 2012, 04:35 PM -0700):

> I noticed in the ZF2 Skeleton application the following Application module
> configuration section:
>     'controller' => array(
>         'classes' => array(
>             'index' => 'Application\Controller\IndexController'
>         ),
>     ),
>
> This SEEMS to make "index" an alias of
> "Application\Controller\IndexController", which is then used in the routes
> section whenever a controller is specified.  If I'm wrong here, please let
> me know.
>
> Then, I see Matthew Weier O'Phinney do something similar:
>     'controller' => array(
>         'class' => array(
>             'application-page'                      =>
> 'Application\Controller\PageController',
>             'Application\Controller\PageController' =>
> 'Application\Controller\PageController',
>         ),
>     ),
>
> However, in his routes, he specifies a full controller name:
> Application\Controller\PageController.

I did both, so I'd have the flexibility; I'm still working out some
ideas.

Basically, the ServiceManager is treating the controller name it
receives from the route match as a service name -- so you simply need to
ensure that that service name resolves somehow.

> I did it both ways and even commented out my whole controller section
> and it didn't seem to make any difference.  The only thing that
> appeared to make my routes use the correct controller was to specify a
> full controller name like Matthew did.

Both should work; if not, we need to add some tests. I'm pretty sure
I've tried both at this point.

> I also know from another thread here that Matthew has advised against
> using aliases, which may or may not be applicable here....I'm not
> sure.

Using aliases was a problem when we were using DI, and specifically
before we had the view layer in place, as there wasn't an easy way to
auto-determine the view script. At this time, however, it should not be
an issue; either way should work.

> Anyway, I would, again, like some clarification on the use of the
> controller section, whether or not its necessary, what it's used for,
> and whether or not aliases should or could be used.

The controller section of configuration is used by
Zend\Mvc\DispatchListener and Zend\Mvc\Service\ControllerLoaderFactory.
It's meant to ensure that once we have a controller in the route
matches, we don't try to instantiate just _anything_, which was the case
prior to beta4; any controller matched it would try to pull from DI,
which meant you could have a URL like "http://foo.tld/Some\Classname/action",
and it would merrily try to create Some\Classname. With the new system,
you have to specify controllers via the controller array. You can do
this using either of the following:

    'controller' => array(
        'classes' => array(
            'name' => 'class\name\pairs',
        ),
        'factories' => array(
            'name' => /* callable or factory class name */,
        ),
    ),

In the "classes" case, you're basically setting aliases. Factories are
useful when you want to inject dependencies.

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


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

Re: Configuring a Module's Controller Classes in ZF2...

travis
Matthew,

Thanks for your reply.  It was very helpful.  I went back into my config file and found TWO controller sections, one with "classes" and one with "class".  Stupidly, I was trying all sorts of different things with the one that said "class" and that's why my results were as they were.  Too much copying and pasting and rewriting over the last few days and weeks.

So, it looks like my results now match with yours...using an alias or a full namespace/classname works...so long as you write what's expected...!  ;-)

I posed a related question here about view script template paths....if you haven't seen it yet, you may want to take a look.  In the meantime, I'll carefully look over my code and make sure there are no more blockheaded mistakes.

Thanks again...!
Loading...