Question

I have created a Module with a Controller. My module is called 'App\Module\Foo' and the controller is called 'App\Controller\Foo'. I have configured Zend trough the application.config.php by using Zend\Mvc\Application::init(require 'config/application.config.php')->run();. (I have setup a custom launcher, quite similar to the ZF2 Skeleton one. )

'router' => array(
    'routes' => array(
        'Foo\Module\Index' => array(
            'type' => 'segment',
            'options' => array(
                'route' => '[/]',
                'constraints' => array(
                    'controller' => 'Foo\Controller\Index'
                )
            )
        )
    )
)

From my understanding this configuration will redirect all requests to the web root to the Foo\Controller\Index controller. Does this really have to be inside the Foo\Module\Index array key or can I replace the key by any name I think is fitting for that route? (As long as it's unique) And if this is true, does that mean I can have 1 big module with multiple routes?

Also, how does it know what method to call on that Controller? In the Zend reference there doesn't seem to be a proxy which takes some arguments (like action) and sends it to the right method inside your controller. It seems like the ZF2 knows by magic what method to call.

Another thing that bothered me was how does the Framework know where the View is located? The official reference says it'll look for {controller name}/{action name} but where will it look, and what if I don't have an action variable?

Also am I right when I am thinking a Model is basically just a Data structure you can use (Like a User class, or a Permissions class) and a Form is a class processing actual POST data being sent in trough a HTML form?

Was it helpful?

Solution

  1. The Controller you assign within your Route-Configuration is completely up to you. Remember though, that in reality you're only assigning a key. So 'controller' => 'ctrl-foo' could be valid, too, as long as there is a controller invokable 'ctrl-foo' => 'MyNamespace\Controller\FooController assigned.

  2. You assigned the Controller under the key constraints - this is wrong - it needs to be under defaults. Then the array becomes 'defaults => [ 'controller' => 'foo', 'action' => 'bar' ]. This would direct your route to the controller assigned by key foo to the barAction() of said controller. A constraint on the other hand would be like an on-the-fly validation. For example you assign your route with an ID parameter 'route' => '/:id' then you could tell the Router to just allow numeric values via 'constraints' => [ 'id' => '\d+' ]. Please note that the array syntax is PHP 5.4+ only

  3. You always have an "action variable". The ViewManager is configured via exactly that configuration key view_manager. The View will be looked up within the template_path_stack

  4. A Model can be just a POPO as you mentioned. But a Model can be more, too. This is part of MVC understanding though, which i'm not going to go into detail here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top