0

I'm trying to understand all the configuration necessary to get my routing working in Zend Framework 2, and I can't help but wonder if I am making this more complicated than necessary.

I am working on a simple app that will follow a very simple convention:

/:module/:controller/:action

I've already created and wired up my module, "svc" (short for "service)". I then created a second controller, the "ClientsController", and I can't get the routing to pass through my requests to, e.g., /svc/clients/list to ClientsController::listAction().

As I'm wading through hundreds of lines of configuration, in deeply nested arrays, I'm thinking--isn't there some way to just have a default mapping of my URLs to /:module/:controller/:action ?

Thanks for any assistance. I'm going off of the Zend Framework 2 Quick Start, which walked me through creating a new module and then adding a controller to that module. But when I tried to add second controller to that module, I am tripping over the routing.

Update: I didn't catch this the first time through, but apparently this is supposed to be a feature of the Zend Framework Skeleton app. From the quick start guide:

ZendSkeletonApplication ships with a “default route” that will likely get you to this action. That route basically expects “/{module}/{controller}/{action}”, which allows you to specify this: “/zend-user/hello/world”

That's exactly what I want! But I can't get it to work.

It lists an incomplete module.config.php, with a comment at the bottom about putting "other configuration" here. I tried to figure out what that "other configuration" is, and wound up with this:

return array(
    'svc' => array(
        'type'    => 'Literal',
        'options' => array(
            'route'    => '/svc',
            'defaults' => array(
                'controller'    => 'svc\Controller\Index',
                'action'        => 'index',
            ),
        ),
        'may_terminate' => true,
        'child_routes' => array(
            'default' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/[:controller[/:action]]',
                    'constraints' => array(
                        'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                    ),
                ),
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'svc\Controller\Clients' => 'svc\Controller\ClientsController',
        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'album' => __DIR__ . '/../view',
        ),
    ),
);

JFYI, here is what my controller looks like.

namespace svc\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class ClientsController extends AbstractActionController {

    public function indexAction() {
        return new ViewModel();
    }

    public function anotherAction(){

        return new ViewModel();
    }
}

My routes are not working. I get "route not found" when I try to pull up any of my routes.

2

1 Answer 1

0

It lists an incomplete module.config.php, with a comment at the bottom about putting "other configuration" here. I tried to figure out what that "other configuration" is, and wound up with this:

If your module.config.php really looks like that then it won't work, routes is an array of routes defined in the router key, your config contains no such spec, try replacing it with this

return array(
    // routes
    'router' => array(
        'routes' => array(
            'svc' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/svc',
                    'defaults' => array(
                        'controller'    => 'svc\Controller\Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                                 // add the default namespace for :controllers in this route
                                 '__NAMESPACE__' => 'svc\Controller',
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),    
    'controllers' => array(
        'invokables' => array(
            'svc\Controller\Clients' => 'svc\Controller\ClientsController',
        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'album' => __DIR__ . '/../view',
        ),
    ),
);
Sign up to request clarification or add additional context in comments.

8 Comments

So I tried that and now I get: "clients(resolves to invalid controller class or alias: clients)". This is at the URL zf-sample.localhost/svc/clients/another. So apparently I need to somehow alias "clients" to my controller class... right? But how do I do that?
Ok, so I changed this 'controllers' => array( 'invokables' => array( 'svc\Controller\Clients' => 'svc\Controller\ClientsController', ), ), to this: 'controllers' => array( 'invokables' => array( 'clients' => 'svc\Controller\ClientsController', ), ), And that seemed to do the trick. Is that right? Other than that change, I've got exactly what you posted.
no, as @Sam pointed out earlier, you'll need to add the __NAMESPACE__ to your route config, see my edit.
Yes, that would be another way to do it. Doing it that way, if you ever have another module that also defines a Clients controller in the same manner, one of them will disappear when configs are merged. May not be an issue for your app, but something to keep in mind
Great, thanks! It's working now. Clarifying question: I see you put child_routes at $returnedArray['router']['routes']['svc']['child_routes']. The Quick Start has child_routes at $returnedArray['svc']['childroutes']. Why the difference? It makes sense to my that child_routes would be a child of routes, so why would the quick start put it as a child of svc (and a sibling of routes)? Here's the quick start link again: framework.zend.com/manual/2.0/en/modules/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.