如何在 Symfony 路由中用“-”替换空格 (%20)

问题描述

我想在 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”

我该怎么做?

解决方法

通常这对数据库中的“slug”字段最有效,你可以像“/receta/{slug}”和你的数据库“slug”字段“pollo-agridulce-chino”一样使用它。 您可以手动添加“slug”或使用类似

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

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...