【问题标题】:Symfony 2/3 Route not found with trailing slash找不到带有斜杠的 Symfony 2/3 Route
【发布时间】:2017-01-04 16:46:49
【问题描述】:

我有以下路线:

product_category:
  path: /{grandFatherSlug}/{fatherSlug}/{childSlug}
  defaults: { _controller: ProductCategory:productCategoryPage, fatherSlug: "",  childSlug: ""}
  options:

由于 SEO 建议,我希望这种路线以斜杠结尾。 但我不能让它工作。我试过了:

product_category:
  path: /{grandFatherSlug}/{fatherSlug}/{childSlug}/
  defaults: { _controller: ProductCategory:productCategoryPage, fatherSlug: "",  childSlug: ""}
  options:

但是我得到了一个找不到路由的异常并且:

product_category:
  path: /{grandFatherSlug}/{fatherSlug}/{childSlug}/
  defaults: { _controller: ProductCategory:productCategoryPage, fatherSlug: "",  childSlug: ""}
  requirements:
    childSlug: .+
    fatherSlug: .+
    grandFatherSlug: .+

但是我得到了带有斜杠的最后一个参数,这让我在控制器中调用 str_replace 来删除它。

是否有任何简单的解决方案来实现这种行为?

谢谢

【问题讨论】:

标签: symfony


【解决方案1】:

您是否尝试在正则表达式中避免使用/

product_category:
  path: /{grandFatherSlug}/{fatherSlug}/{childSlug}/
  defaults: { _controller: ProductCategory:productCategoryPage, fatherSlug: "",  childSlug: ""}
  requirements:
    childSlug: [.+^\/]
    fatherSlug: .+
    grandFatherSlug: .+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    • 2012-11-16
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多