Php Zend Framework 2段路线所需段

Php Zend Framework 2段路线所需段,php,routing,zend-framework2,zend-route,zend-framework-routing,Php,Routing,Zend Framework2,Zend Route,Zend Framework Routing,我正在尝试创建一个(或两个)路由,允许我使用以下URL格式调用两个不同的操作 mydomain.com/profile mydomain.com/profile/1234/something 对于第二种格式,1234应该是必需的整数值,而something应该是可选字符串。第一种格式很简单,使用文字路由。我想我可以为第二种格式添加一个段子路由,但我无法让它工作。我试图省去第一种格式,只做了第二种带有分段路线的格式,但我也没有成功 以下是我尝试过的: 'profile' => array(

我正在尝试创建一个(或两个)路由,允许我使用以下URL格式调用两个不同的操作

mydomain.com/profile
mydomain.com/profile/1234/something
对于第二种格式,
1234
应该是必需的整数值,而
something
应该是可选字符串。第一种格式很简单,使用文字路由。我想我可以为第二种格式添加一个段子路由,但我无法让它工作。我试图省去第一种格式,只做了第二种带有分段路线的格式,但我也没有成功

以下是我尝试过的:

'profile' => array(
    'type' => 'Zend\Mvc\Router\Http\Literal',
    'options' => array(
        'route' => '/profile',
        'defaults' => array(
            'controller' => 'User\Controller\User',
            'action' => 'profile'
        )
    ),
    'child_routes' => array(
        'profile_view' => array(
            'type' => 'Zend\Mvc\Router\Http\Segment',
            'options' => array(
                'route' => '/:code[/:username]',
                'constraints' => array(
                    'code' => '[0-9]*',
                    'username' => '[a-zA-Z0-9_-]*'
                ),
                'defaults' => array(
                    'controller' => 'User\Controller\User',
                    'action' => 'view_profile'
                )
            )
        )
    )
)
对于
mydomain.com/profile
,我得到以下错误:

Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'No RouteMatch instance provided'
对于
mydomain.com/1234/something
,我得到以下错误:

Fatal error: Uncaught exception 'Zend\Mvc\Router\Exception\InvalidArgumentException' with message 'Missing parameter "code"'
委员会:

如果段是可选的,则应使用括号将其包围。作为 例如,“/:foo[/:bar]”将匹配一个“/”,后跟文本和赋值 它的键是“foo”;如果找到任何其他“/”字符,则 最后一条之后的文本将分配给键“bar”

这不正是我在做的吗?如果我注释掉约束,上述错误保持不变


我错过了什么?提前谢谢。

我相信您缺少了
可以终止
选项,如下所示:

'profile' => array(
    'type' => 'Zend\Mvc\Router\Http\Literal',
    'options' => array(
        'route' => '/profile',
        'defaults' => array(
            'controller' => 'User\Controller\User',
            'action' => 'profile'
        )
    ),
    'may_terminate' => true,
    'child_routes' => array(
    ...

您得到的第二个异常
缺少参数“code”
来自组装,而不是匹配。这意味着您在装配管线时未提供代码
纵断面图/纵断面图


顺便说一下,您不必在子路由前加上父路由的名称。只需将子路由称为“视图”,并将其组装为
profile/view

我又玩了一些。经过调试,我还是搞不懂。我试图给
code
参数添加一个默认值,这似乎奏效了。我不知道这到底是为什么,它是如何有意义的。对我来说,如果我在URL中为URL中的参数提供了值,那么是否指定了默认值并不重要。然而,很明显,这确实很重要——或者至少在这个案例中是如此

这是工作代码。我还更新了代码参数的正则表达式

'profile' => array(
    'type' => 'Zend\Mvc\Router\Http\Literal',
    'options' => array(
        'route' => '/profile',
        'defaults' => array(
            'controller' => 'User\Controller\User',
            'action'     => 'profile',
        ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'view' => array(
            'type' => 'Zend\Mvc\Router\Http\Segment',
            'options' => array(
                'route' => '/:code[/:username]',
                'constraints' => array(
                    'code' => '\d+',
                    'username' => '[a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                    'controller' => 'User\Controller\User',
                    'action' => 'viewProfile',
                    'code' => 0,
                ),
            ),
        ),
    ),
),

Bittarman认为,出现这种情况的原因有两个。他说,主要原因是:

<Bittarman> ezio, that looks like what happens when you make a new phprenderer rather than using the configured one
ezio,当您创建一个新的phprenderer而不是使用配置的phprenderer时,会发生这种情况
显然,我有一个较小的理由: 我在布局中使用了$this->url()。因为没有路由匹配——一个404错误——它在所有404错误上爆炸了

<Bittarman> for all of those things, stop doing if (!$this->url() == $this->url('home') etc
<ezio> ummm when it's home my business partner wants the whole order changed so google can hit on a bunch of keyords
<Bittarman> instead, in the VIEW for home, set what you need.
<ezio> how would i set the second example where i'm trying to highlight the currently-being-used menu item
<Bittarman> so, in your layout, you just want echo $this->headTitle()->setSeparator(' - ')->setAutoEscape(false)
<Bittarman> ezio, use the placeholder view helper
<ezio> okay
<ezio> thanks Bittarman ... tearing my hair out is not fun when you're already losing so much of it :p
<Bittarman> <?= $this->placeholder('currently-being-used') ?>
<Bittarman> and in your view, <?php $this->placeholder('currently-being-used')->append('foo') ?>
<ezio> sense making a lot you are
对于所有这些事情,如果(!$this->url()==$this->url('home')等等,请停止执行
嗯,当它在家的时候,我的商业伙伴想改变整个订单,这样谷歌就可以找到一堆关键字了
相反,在主视图中,设置所需内容。
我如何设置第二个示例,其中我试图突出显示当前正在使用的菜单项
因此,在布局中,您只需要echo$this->headTitle()->setSeparator('-')->setAutoEscape(false)
ezio,使用占位符视图帮助器
可以
谢谢比塔曼…当你已经失去那么多的时候,扯掉我的头发是不好玩的:p
在你看来,
你很有意义

我的解决方案:将可选参数的默认值设置为空字符串

我遇到了一个类似的问题,我通过设置可选参数的默认值来解决它(在您的示例中,“username”为空字符串“”;我的解决方案:

'users' => array(
    'type' => 'segment',
    'options' => array(
        'route' => '/users[/:user_id]',
        'constraints' => array(
            'user_id' => '[0-9]*',
        ),
        'defaults' => array(
            'controller' => 'My\Controller\User',
            'user_id' => ""
        )
    ),

不幸的是,这没有帮助。
mydomain.com/profile
可以工作,但是
mydomain.com/profile/
mydomain.com/profile/1234
mydomain.com/1234/something
都会给我一个错误,说明
code
参数丢失。没有满足
mydomain.com/profile/
的路由s尾随斜杠将您置于子路由中,然后需要
:code
。如果从
:code
中删除约束,此路由是否有效?否则,我将禁用
:用户名
,只需使用
/profile/1234
/profile
使路由工作。是的,我不期望g
mydomain.com/profile/
可以工作,但我想知道为什么它在没有提供必需参数的情况下甚至与子路由匹配。无论如何,删除约束并没有什么区别。从路由中完全删除用户名也没有什么区别。我仍然会收到相同的错误消息。我已经检查了所有对于我的其他路由,它们都不应干扰我的新路由。我已尝试禁用一些,但结果相同。它无论如何都与正确的路由匹配,因此这不是问题。感谢您迄今为止的帮助。可能值得深入研究段路由代码,并在引发异常之前检查值和实例状态n、 你是对的,错误在于装配和不匹配。然而,这对我来说仍然没有意义。尽管如此,我似乎已经找到了问题所在(尽管我不理解)。通过在代码参数中添加默认值,一切似乎都正常。奇怪!另外,感谢您提醒我命名。实际上,我这样命名只是巧合。:)添加默认参数是有效的,因为现在您通过默认值为代码提供了一个值。尽管在调用Assembly方法时您应该提供一个值。有趣的是,我可以在我的控制器中很好地获得
code
参数,它不是0(即默认值)。相反,这是我在URL中写入的值,这是我想要的。这不应该标记为答案;它只是一个创可贴。我在下面发布了我认为是答案的内容。请查看。这如何回答OP?