【问题标题】:Zend Framework3 similar routes conflictZend Framework3 类似路由冲突
【发布时间】:2019-01-29 10:29:08
【问题描述】:

我正在使用 ZF3,在 Post 模块的 module.config.php 文件中,我有这两条路线之一,

'create-post' => [
    'type' => Literal::class,
    'options' => [
        // Change this to something specific to your module
        'route' => '/post/create',
        'defaults' => [
            'controller' => Controller\PostController::class,
            'action' => 'create',
        ]
    ],
    'may_terminate' => true,
    'child_routes' => [
        // You can place additional routes that match under the
        // route defined above here.
    ],
],

'post' => [
    'type' => Segment::class,
    'options' => [
        // Change this to something specific to your module
        'route' => '/post[/:postId]',
        'defaults' => [
            'controller' => Controller\PostController::class,
            'action' => 'show',
        ],
        'constraints' => array(
            'postId' => '\d{4}'
        )
    ],
    'may_terminate' => true,
    'child_routes' => [
        // You can place additional routes that match under the
        // route defined above here.
    ],
]

现在当我访问 http://localhost:8080/post/create 时,它可以工作,但是当我访问 http://localhost:8080/post/32 时,它不起作用。它说 404 错误,找不到页面。

非常感谢任何帮助。

【问题讨论】:

  • ID 必须是 4 位数字,不是吗?
  • 不,可以是任意4位数字
  • 这和我说的有什么不同? localhost:8080/post/32 不是 4 位,是 2,所以不匹配?
  • 我的意思是最多 4 位数字
  • 现在可以用了,我把它改成了2

标签: php zend-framework routes zend-framework3


【解决方案1】:

根据@jon Stirling 对我的问题的评论,我更改了对发布路线的限制并且它起作用了。

将 'postId' => '\d{4}' 更改为 'postId' => '\d{1,4}'

'post' => [
                'type'    => Segment::class,
                'options' => [
                    // Change this to something specific to your module
                    'route'    => '/post[/:postId]',
                    'defaults' => [
                        'controller'    => Controller\PostController::class,
                        'action'        => 'show',
                    ],
                    'constraints' => array(
                        'postId' => '\d{1,4}'
                    )
                ],
                'may_terminate' => true,
                'child_routes' => [
                    // You can place additional routes that match under the
                    // route defined above here.
                ],
            ]

【讨论】:

  • 顺便说一句,当您想使用 ID 时,为什么要从字面上使用 [0-9]+(任意数量的字符 0 到 9)的每个示例中更改它。另外,您为什么不将child_routes 用于不同的操作? (例如,showAction 位于 /post 路由上,addAction 位于 /create 子路由上)
  • 我在子路由方面遇到了一些问题,这就是我不使用它的原因。例如:我有一条名为“post”的路线/post/33,它的子路线名称为“edit”,所以它会变成/post/33/edit,现在我如何使用任何助手从视图中调用“edit” .我在锚标记的href中尝试了像$this->url('edit')这样的'url'助手,但它说'找不到名称为'edit'的路由'?!!
  • 如果你有post(路由/post)、子view(路由/:id)和那个edit(路由/edit)的子,完整的路由NAMEpost/view/edit,其中view 名称需要一个ID。所以url 视图助手应该是:$this->url('post/view/edit', ['id' => $id])。查看路线是$this->url('post/view', ['id' => $id]),普通索引页面是$this->url('post')(那里没有参数)。如果你愿意,我可以给你举个例子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-15
  • 2018-03-25
  • 2018-06-07
  • 2018-05-23
  • 2013-06-05
相关资源
最近更新 更多