1

I have the following routing config in my project:

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route' => '/',
                'defaults' => array(
                    '__NAMESPACE__' => 'MyApp\Controller',
                    'controller' => 'Default',
                    'action' => 'default',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'api' => array(
                    'type' => 'Zend\Mvc\Router\Http\Segment',
                    'options' => array(
                        'route' => 'api/:action[/:id]',
                        'defaults' => array(
                            '__NAMESPACE__' => 'MyApp\Controller',
                            'controller' => 'Api',
                            'action' => 'index',
                        ),
                    ),
                    'may_terminate' => true,
                ),
                'default' => array(
                    'type' => 'Zend\Mvc\Router\Http\Segment',
                    'options' => array(
                        'route' => '[:id]',
                        'defaults' => array(
                            '__NAMESPACE__' => 'MyApp\Controller',
                            'controller' => 'Default',
                            'action' => 'default',
                        ),
                    ),
                    'may_terminate' => true,
                ),
            ),
        ),
    ),
),

When I make a call to http://localhost/api/foo/bar I get the response from the DefaultController. I have stripped my application until this is the only route (removed home/default, and made it an only route for the application) but it is ignored.

The desired outcome is that calls to /api... go to the Api controller, but all other calls go to the Default Controller.

There are no errors being thrown as far as I can see (looking at apache2 logs)

Any suggestions as to what might be wrong?

6
  • When you say it is ignored, what do you mean? When you removed the 'default' route, what did you get when you visited that URL? Apr 25, 2015 at 13:39
  • Blank page in browser, no error in logs - literally nothing Apr 25, 2015 at 14:51
  • Well, blank page usually means an error. Which logs have you checked? Apr 25, 2015 at 14:53
  • apache logs, not sure where else to check really Apr 25, 2015 at 14:54
  • What url is not matched? /api?
    – Xerkus
    Apr 26, 2015 at 2:26

1 Answer 1

1

I think it's because your default route id has no constraints, so it's matching everything. Try adding this after your default route.

'constraints' => ['id' => '[0-9]'],

8
  • I'm afraid that wouldn't work - I need it to match anything that's not matched by the segment routes - it's working as a CMS - and the "id" can be anything (except "api" naturally) - essentially that route is a catch-all Apr 25, 2015 at 14:52
  • Okay, then your constraint needs to be a clever regex then. See stackoverflow.com/questions/16930563/…
    – dualmon
    Apr 25, 2015 at 15:01
  • I may have to go with this, but it's a bit of an inconvenience that I have to add an exception to the route every time I add another segment Apr 26, 2015 at 18:22
  • Unless I make the Default Controller handle errors, and intercept the 404 instead of routing a default catch-all. This seems messy to me though Apr 26, 2015 at 18:25
  • Or you make a distinguishing segment before the default route like /content or /post or /whatnot
    – dualmon
    Apr 26, 2015 at 18:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.