Laravel 重定向的几种方法
重定向URL
路由:
Route::get('itsolutionstuff/tags', 'HomeController@tags');
控制器:
public function home()
{
return redirect('itsolutionstuff/tags');
}
重定向回上一页
public function home()
{
return back();
}
//或者
public function home2()
{
return redirect()->back();
}
重定向到命名路由
路由:
Route::get('itsolutionstuff/tags', array('as'=> 'itsolutionstuff.tags', 'uses' => 'HomeController@tags'));
控制器:
public function home()
{
return redirect()->route('itsolutionstuff.tags');
}
使用参数重定向到命名路由
路由:
Route::get('itsolutionstuff/tag/{id}', array('as'=> 'itsolutionstuff.tag', 'uses' => 'HomeController@tags'));
控制器:
public function home()
{
return redirect()->route('itsolutionstuff.tag',['id'=>17]);
}
重定向到控制器
public function home()
{
return redirect()->action('HomeController@home');
}
重定向到带有参数的控制器
public function home()
{
return redirect()->action('App\Http\Controllers\HomeController@home',['id'=>17]);
}
使用会话数据重定向
我们还可以在控制器方法中用路由或url重定向时传递闪过的会话消息,如下所示。
public function home()
{
return redirect('home')->with('message', 'Welcome to PHP.cn!');
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: