What is 'may_terminate' in zend framework 2?

北战南征 提交于 2019-11-28 20:26:17

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!