如何解决Laravel自定义中间件
我正在尝试使用中间件来实现这一目标
web.php
Route::get('/test','TestController@index')->middleware('TestLogin');
重定向到/ test,如果找到会话
Route::get('/test1','TestController@index1')->middleware('TestLogin');
如果已设置会话,则重定向到test1
中间件-TestLogin
public function handle($request,Closure $next)`
{
if($request->session()->get('Username'))
{
return redirect()->route(\);
// what to write here to redirect to the path its being called from
}
return redirect()->route('login');
}
不想使用默认的身份验证中间件
解决方法
您的中间件应该检查登录(会话),如果找不到,则重定向到登录页面,否则它将通过请求。像这样:
public function handle($request,Closure $next)
{
// No login session,redirect
if(!$request->session()->get('Username'))
{
return redirect()->route('login');
}
// Pass the request down the rest of pipeline
return $next($request);
}
您应该考虑使用内置authentication system in Laravel本身,它将节省大量代码,并且可以确保从安全角度正确处理身份验证。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。