Laravel 使用 redirect 进行重定向

PHP 投稿 111400 0 评论

Laravel 使用 redirect 进行重定向

Laravel在路由中直接从定向跳转

动态URL跳转

Route::get('article/{id}', function($id){ 
    return Redirect::to($id, 301); 
});

Route::get('code-of-{user}', function($user){ 
    return Redirect::to($user, 301); 
});

Route::get('amp/code-of-{user}', function($user){ 
    return Redirect::to('amp/'.$user, 301); 
});

Laravel永久迁移:301重定向

在controller中的方法中,主要用到以下代码:

return redirect('', 301);

Laravel临时迁移:302重定向

在Laravel中,redirect默认重定向方式为临时迁移,http状态码是302

直接地址:

return redirect('/home');

用路由名字:

return redirect()->route('route.name');

跳转上一个页面:

return redirect()->back();

有时候我们提交from表单的时候,可能会提交失败,如果失败了,又不想重新填写一遍,这样我们就需要用到携带参数跳转了:

return redirect('/vien')->withInput();

而我们有时候需要登录后跳转到上一个访问的页面,在laravel的auth模块中其实是这样定义的:

return redirect()->intended();

编程笔记 » Laravel 使用 redirect 进行重定向

赞同 (117) or 分享 (0)
游客 发表我的评论   换个身份
取消评论

表情
(0)个小伙伴在吐槽