我在很多网址之前都有一个位置前缀,例如example.com/london/
问题是,当我想使用别名在我的控制器中重定向时,如下所示:
if($validator->fails()) {
return Redirect::route('register')->withErrors($validator);
}
它重定向到example.com/%7Blocation%7D/register
而不是example.com/london/register
是否有一个简单的解决方法,以便它包含正确的位置,或者我每次重定向时都必须手动输入该位置?
我的routes.php
Route::group(['prefix' => '{location}'], function() {
Route::get('/', 'LocationController@home');
Route::get('/register', array('as' => 'register', 'uses' => 'AuthController@getRegister'))->before('guest');
Route::post('/register', array('uses' => 'AuthController@postRegister'))->before('csrf');
})
{location}
像普通路由参数一样处理,因此您可以将其作为第二个参数传递:
return Redirect::route('register', 'london')->withErrors($validator);
由于它是一个路由参数,您也可以像一个参数一样检索它。与Route::input()
.这意味着,如果要重定向到与当前路由具有相同前缀的路由:
return Redirect::route('register', Route::input('location'))->withErrors($validator);
您还可以添加默认值:Route::input('location', 'london')