26

I am beginner working with Zend. I have seen may_terminate in module route configuration. I don't understand what it is for. According to ZF2 official docs,

the option “may_terminate” hints to the router that no other 
segments will follow it.

Still I don't get the meaning of no other segments will follow it. What is it here? Can anyone please explain it with small example?

1 Answer 1

52

The may_terminate option will indicate to the router that 'this' route is able to be matched solely on the value of its route; even when it defines child_routes.

Consider the following example route configuration.

'router' => [
    'routes' => [

        'home' => [
            'type' => 'literal',
            'options' => [
                'route' => '/home',
            ],
            'may_terminate' => false,
            'child_routes' => [

                'foo' => [
                    'type' => 'literal',
                    'options' => [
                        'route' => '/foo',
                    ],
                ],
            ],
        ],
    ],
],

There is some ambiguity in the above configuration, which only occurs with routes that define children. Do we want to allow our users to match on two routes or just one?

We could allow matching on just the /home part; which would mean we have two routes both /home and /home/foo or we might only want to allow /home/foo.

This is where the may_terminate option is used. If we browsed to /home in our browser, when the routing occurs the router cannot regard the home route as a matchable route as may_terminate = false. In ZF2 terminology the router cannot "terminate" at this route and continues on searching for a match into the child_routes, which will fail and a 404 error will be raised.

So by modifying the value of the may_terminate option in the above example, we can change the routes that can be matched on.

may_terminate = true

  • home
  • home/foo

may_terminate = false

  • home/foo
5
  • Just a question: Is the definition of the "foo" route wrong? In my option it should be just "foo" instead of "/foo". Else you will have a double slash. Or am i wrong? Commented Dec 26, 2013 at 19:54
  • @Stillmatic1985 I does seem that way at first glance, I didn't check my config but did see this example in the docs
    – AlexP
    Commented Dec 26, 2013 at 19:59
  • Hmm ok. Didnt tested it but it looks wrong. if you had "/home" as home route everything looks great but i think if you only have "/" as home route the child route shouldnt have a starting slash. I could be wrong. ;) Commented Dec 26, 2013 at 20:02
  • @Stillmatic1985 you are right. The example is read a bit tricky... It's the factory creating and actually the first child_routes doesn't belong to the home route at all. Look closely ;) Didn't really look at the code too closely, good catch
    – Sam
    Commented Dec 26, 2013 at 20:45
  • Thank you! This helped me to find out what I was doing wrong.
    – smozgur
    Commented May 9, 2015 at 16:58

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.