0
votes

I'm trying to achieve the routing explained in below but having a lot of trouble (the documentation is like a foreign language). Should routing for each module be kept separate in that module's config file?

  • "route": Module/Controller/Action
  • "/": Application/Index/index
  • "/:example": Application/Campaign/index (with param "campaign" = [example])
  • "/admin": Admin/Admin/index
  • "/admin/login": Admin/Access/login
  • "/admin/:controller/:action": Admin/[defined]/[defined]

I've tried to understand and use the skeleton application to do this and everything works up to the "/admin/controller/action" route where the script times out (I'm guessing there's a recursive loop in there somewhere). My route definitions are:

'home' => array(
            'type' => 'literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller'    => 'Application\Controller\Index',
                    'action'        => 'index',
                ),
            ),
        ),
        'campaign' => array(
            'type' => 'segment',
            'options' => array(
                'route'    => '[/:campaign]',
                'constraints' => array(
                    'campaign' => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                    'controller'    => 'Application\Controller\Campaign',
                    'action'        => 'index',
                    'campaign'      => ''
                ),
            ),
        ),
        'admin' => array(
            'type' => 'segment',
            'options' => array(
                'route'    => '/admin',
                'defaults' => array(
                    'controller'    => 'Admin\Controller\Admin',
                    '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(
                        ),
                    ),
                ),
            ),
        ),
1
Add the urls you're trying to access and which one of them times-out. Based on nothing in particular but a gut feeling, I'd say the timeout doesn't stem from the routing itself. Maybe some faulty code in the controllers action or something altogether unrelated like server config. - guessimtoolate
Problem is fixed now. Like you said, it was nothing to do with the router. I had forgotten that I had print_r($this) in the 404.phtml file so the memory was timing out. - dhh

1 Answers

0
votes

1) Yes, you should separate routing config to modules, ZF2 will merge it automatically. So you need to store home and campaign routes in Application/config/module.config.php and admin route in Admin/config/module.config.php

2) I don't see any global mistakes in your config. Two small things:

2.1) You can use literal instead of segment for admin parent route

2.2) If you want to use :controller as a route param, be sure to make shortcut for it via invokables section of config. For example:

'controllers' => array(
    'invokables' => array(
        'index' => 'Application\Controller\Index'
        'admin' => 'Admin\Controller\Admin'
    ),
),

Now, for example, url /admin/admin/update is connected with Admin\Controller\Admin->updateAction()

3) I think it's not possible to make some recursive loops using only routing config. So check your code in controllers/services