简体   繁体   English

Laravel 5 Route变量具有空间

[英]Laravel 5 Route variable has space

I have some code in a controller called SearchController and I redirect to a route with some variables that were defined. 我在名为SearchController的控制器中有一些代码,并使用定义的一些变量将其重定向到路由。 The city variable has the spaces removed and a dash added for when the city has two words(ex:Des Moines). 当城市有两个词(例如:得梅因)时,city变量将删除空格并添加一个破折号。 That would return "des-moines". 那将返回“ des-moines”。 I did this because in the URL it was showing up as "des+monies". 我这样做是因为在URL中它显示为“ des + monies”。 But after changing that. 但是改变了之后。 The route does seem to know what to do or it breaks. 该路线似乎不知道该怎么办或已中断。 So how can I tell the route that the {city} can also be things like "des-moines" instead of having "des+moines". 因此,如何告诉路线{city}也可以是“ des-moines”之类的东西,而不是“ des + moines”。 Or is there a better way of doing this. 还是有更好的方法来做到这一点。

Url I want(but breaks and gets error): 我想要的网址(但会出错):

www.domain.com/des-moines-ia-provider1-50301

SearchController(cut down version): SearchController(简化版):

$city =  strtolower(preg_replace('/\s+/', '-', $data[0]->City));
$state = strtolower($data[0]->State);
$provider = strtolower($data[0]->{'Provider Name'});
$zipcode = $data[0]->{'Zip Code'};  

return redirect()->route('search.provider1',[$city,$state,$zipcode])->with('data',$data);

Routes: 路线:

Route::get('{city}-{state}-provider1-{zipcode}',['uses' => 'PagesController@provider1','as' => 'search.provider1']);

Error: 错误:

NotFoundHttpException in RouteCollection.php line 161:

What you could do is replacing in your routes separator that won't be used in in city/state/zipcode. 您可以做的是替换您的路线分隔符,该分隔符将不会在城市/州/邮政编码中使用。

Maybe: 也许:

Route::get('{city}/{state}/provider1/{zipcode}',['uses' => 'PagesController@provider1','as' => 'search.provider1']);

would be better? 会更好? I suppose / is not used in city/state and zipcode 我想/不在城市/州和邮政编码中使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM