Laravel路由-破折号 [英] Laravel Routing - dashes
问题描述
我需要制作一条在变量后加破折号的路线.我想要的内容可以通过代码轻松地解释(这是我尝试过的并且无法正常工作)
I need to make a route that has dashes after variables. What I want is easily explained by code (this is what I tried and it`s not working)
Route::any('tournament/{sportName}/{regionName}/{tournamentName}-odds', array('as' => 'tournament-page', 'uses' => 'HomeController@tournament'));
问题是这部分是"-odds".当我添加该内容时,我会收到此Laravel错误
The the problem is this part "-odds". When I add that I get a Laravel error of this content
$others = $this->checkForAlternateVerbs($request);
if (count($others) > 0)
{
return $this->getOtherMethodsRoute($request, $others);
}
throw new NotFoundHttpException;
我该怎么做(在路径中的参数后添加破折号)?谢谢
How can I do this (add dashes after parameters in routes)? Thanks
推荐答案
问题是,如果在路由参数之后出现以下字符之一:/,;.:-_〜+ * = @ |
,您不能在该route参数中使用它,因为Laravel会调整正则表达式以排除该参数.
The problem is that if after a route parameter comes one of these characters: /,;.:-_~+*=@|
, you can't use it inside that route parameter because Laravel adjusts the regex to exclude that parameter.
我认为这样做的原因是这样的情况: test/{foo}-{bar}
I believe the reason for this is a scenario like: test/{foo}-{bar}
这意味着您显然可以更改URL以不使用route参数内的-
,或使用自行指定适用于
: tournamentName
的正则表达式条件()
That means you could obviously change your URL to not use -
inside the route parameter, or specify the regex condition that applies to the tournamentName
yourself using where()
:
Route::any('tournament/{sportName}/{regionName}/{tournamentName}-odds',
array('as' => 'tournament-page', 'uses' => 'HomeController@tournament')
)->where('tournamentName', '[^\/]+');
这篇关于Laravel路由-破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!