Symfony2:如何在 url 参数的路由文件中制作正则表达式,以便制作干净的 url 名称
Symfony2 : how to make a Regex in routing file for url argument in order to make clean url name
我想为路由参数制作一个正则表达式,这是我的路由文件:
editBatiments:
path: /mypathto/edit/{name}
defaults: { _controller: MyBundle:Mycontroller:editBatiments }
requirements:
name: // put the regex here
methods: GET
此正则表达式用于 删除 space、下划线、标点符号。但是我想 允许 大写和小写的 course.In 事实上,当我继续这条路线时,我的 url 看起来像这样:
localhost/myFolder/mypathTo/edit/Name%20Of%20My%20Bâtiment
我想删除“%20”和space以便有一个url像this:
localhost/myFolder/mypathTo/edit/NameOfMyBâtiment
我怎样才能保持干净url?
我试试这个:
#route file
editBatiments:
path: /mypathto/edit/{name}
defaults: { _controller: MyBundle:Mycontroller:editBatiments }
requirements:
name: "[a-zA-Z0-9-_\/-\s.]+"
methods: GET
但是我有这个错误:
An exception has been thrown during the rendering of a template
("Warning: preg_match(): Compilation failed: invalid range in
character class at offset 16 in
C:\wamp\www\MyFolder\app\cache\dev\classes.php line 848") in
MyBundle:MyFolder:indexBatiments.html.twig at line 60.
这是 indexBatiments.html.twig at line 60
中的代码:
<td>
<a href="{{ path('editBatiments', {'name': batiment.name}) }}">
<button class="btn btn-warning btn-xs">Edit</button>
</a>
</td>
当我走这条路时,有人可以帮助我做一个 cean url?
提前谢谢你。
将您的正则表达式更改为:
name: "[a-zA-Z0-9_\/\s.-]+"
即将 未转义的连字符 保留为字符 class 中的最后一个字符或第一个字符。请注意,字符 class 中间的未转义连字符被视为范围,因此导致您遇到异常。
PS:您可以将正则表达式缩短为:
name: "[\w\/\s.-]+"
编辑: 看起来您也在匹配 unicode 文本。试试这个正则表达式
name: "[\p{L}\p{N}/\s.-]+"
我想为路由参数制作一个正则表达式,这是我的路由文件:
editBatiments:
path: /mypathto/edit/{name}
defaults: { _controller: MyBundle:Mycontroller:editBatiments }
requirements:
name: // put the regex here
methods: GET
此正则表达式用于 删除 space、下划线、标点符号。但是我想 允许 大写和小写的 course.In 事实上,当我继续这条路线时,我的 url 看起来像这样:
localhost/myFolder/mypathTo/edit/Name%20Of%20My%20Bâtiment
我想删除“%20”和space以便有一个url像this:
localhost/myFolder/mypathTo/edit/NameOfMyBâtiment
我怎样才能保持干净url?
我试试这个:
#route file
editBatiments:
path: /mypathto/edit/{name}
defaults: { _controller: MyBundle:Mycontroller:editBatiments }
requirements:
name: "[a-zA-Z0-9-_\/-\s.]+"
methods: GET
但是我有这个错误:
An exception has been thrown during the rendering of a template ("Warning: preg_match(): Compilation failed: invalid range in character class at offset 16 in C:\wamp\www\MyFolder\app\cache\dev\classes.php line 848") in MyBundle:MyFolder:indexBatiments.html.twig at line 60.
这是 indexBatiments.html.twig at line 60
中的代码:
<td>
<a href="{{ path('editBatiments', {'name': batiment.name}) }}">
<button class="btn btn-warning btn-xs">Edit</button>
</a>
</td>
当我走这条路时,有人可以帮助我做一个 cean url?
提前谢谢你。
将您的正则表达式更改为:
name: "[a-zA-Z0-9_\/\s.-]+"
即将 未转义的连字符 保留为字符 class 中的最后一个字符或第一个字符。请注意,字符 class 中间的未转义连字符被视为范围,因此导致您遇到异常。
PS:您可以将正则表达式缩短为:
name: "[\w\/\s.-]+"
编辑: 看起来您也在匹配 unicode 文本。试试这个正则表达式
name: "[\p{L}\p{N}/\s.-]+"