
设置重定向的方法:1、利用“Route::get()”和redirect()重定向URL;2、利用“redirect()->back()”重定向回上一页;3、重定向到命名路由;4、重定向到控制器或带有参数的控制器;5、使用会话数据重定向等。
本教程 *** 作环境:windows7系统、Laravel5版、Dell G3电脑。
Laravel 重定向的几种方法
1、重定向 URL
路由:
Route::get('itsolutionstuff/tags', 'HomeController@tags');控制器:
public function home()
{
return redirect('itsolutionstuff/tags');
}2、重定向回上一页
public function home()
{
return back();
}
//或者
public function home2()
{
return redirect()->back();
}3、重定向到命名路由
路由:
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]);
}4、重定向到控制器
public function home()
{
return redirect()->action('HomeController@home');
}重定向到带有参数的控制器
public function home()
{
return redirect()->action('App\Http\Controllers\HomeController@home',['id'=>17]);
}5、使用会话数据重定向
我们还可以在控制器方法中用路由或url重定向时传递闪过的会话消息,如下所示。
public function home()
{
return redirect('home')->with('message', 'Welcome to PHP.cn!');
}相关推荐:最新的五个Laravel视频教程
以上就是laravel设置重定向的方法有哪些的详细内容,
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)