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 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 1
    public function jump(Request $request)
    {
        // return redirect($url, 301);
        // return redirect()->away('www.learnku.com');
        return redirect()->away($request->url);
    }
4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!