【问题标题】:How to replace spaces (%20) by "-" in Symfony routes如何在 Symfony 路由中用“-”替换空格(%20)
【发布时间】:2021-07-25 02:10:44
【问题描述】:

我想在 Symfony 4.4 路由中用“-”替换空格 (%20) 并删除我的 {slug} 的第一个大写字母。

例如:

RecipeController.php

 /**
 * @Route("/receta/{title}", name="recipe_show", methods={"GET"})
 */
public function show(Recipe $recipe): Response
{
    return $this->render('recipe/show/show.html.twig', [
        'recipe' => $recipe,
    ]);
}

现在我的路线显示了。

https://localhost:8000/receta/Pollo%20agridulce%20chino

但我想展示

https://localhost:8000/receta/pollo-agridulce-chino

在我的 BD 中,我保存“Pollo agridulce chino”

我该怎么做?

【问题讨论】:

    标签: symfony routes symfony4 space symfony-4.4


    【解决方案1】:

    通常这最适用于数据库中的“slug”字段,您可以使用“/receta/{slug}”和数据库中的“slug”字段“pollo-agridulce-chino”。 您可以手动添加“slug”或使用类似

    strtolower(str_replace(' ', '-','Pollo agridulce chino'))
    

    当您将标题保存在数据库中时

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 2018-01-16
      相关资源
      最近更新 更多