php 使用 Laravel Auth 中间件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32164695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Laravel Auth middleware
提问by harish
Laravel 5.1 really had minimal documentation.. I need clear idea about how to protect routes using Auth middileware..
Laravel 5.1 的文档确实很少。我需要清楚地了解如何使用 Auth 中间件保护路由。
Documentation tells to add "middleware" => "auth" parameter to route. or can do
文档告诉将“中间件”=>“身份验证”参数添加到路由。或者可以做
public function __construct()
{
$this->middleware('auth');
}
But How to use Auth middleware for actual user authentication and auto redirection to /login from protected routes ??
但是如何使用 Auth 中间件进行实际用户身份验证并从受保护的路由自动重定向到 /login ?
回答by Angel M.
In Kernel.php - there are registered middlewares under protected $routeMiddleware like this:
在 Kernel.php - 在受保护的 $routeMiddleware 下有注册的中间件,如下所示:
/**
* The application's route middleware.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => 'App\Http\Middleware\Authenticate',
'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
];
You can see 'auth' is registered for using App\Http\Middleware\Authenticate.
您可以看到 'auth' 已注册使用 App\Http\Middleware\Authenticate。
Then you can follow this path - if you open /app/Http/Middleware/Authenticate.php, you will find public function handle:
然后你可以按照这个路径 - 如果你打开/app/Http/Middleware/Authenticate.php,你会发现公共函数句柄:
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($this->auth->guest())
{
if ($request->ajax())
{
return response('Unauthorized.', 401);
}
else
{
return redirect()->guest('auth/login');
}
}
return $next($request);
}
and here is where redirection is managed, and you can modify it for your own needs, or you can create custom middleware.
这里是管理重定向的地方,您可以根据自己的需要修改它,也可以创建自定义中间件。
finally - as it is written in documentation - in the controller, which will need to be authenticated, you will add
最后 - 正如文档中所写的那样 - 在需要进行身份验证的控制器中,您将添加
public function __construct()
{
$this->middleware('auth');
}
You can create a custom middleware if provided ones do not suit your needs.
如果提供的中间件不适合您的需要,您可以创建自定义中间件。
回答by Bruno Pires Lavigne Quintanilh
On laravel 5.2 if you want to hide the registration form or the login form views you should use your middleware as:
在 laravel 5.2 上,如果你想隐藏注册表单或登录表单视图,你应该使用你的中间件:
$this->middleware('mymiddleware', ['only' => ['register', 'showRegistrationForm', 'login', 'showLoginForm']]);
OR
或者
$this->middleware('mymiddleware', ['except' => ['register', 'showRegistrationForm', 'login', 'showLoginForm']]);
That is because the register and login routes are the post methods on the AuthController while showXxxxForm are the form views.
那是因为 register 和 login 路由是 AuthController 上的 post 方法,而 showXxxxForm 是表单视图。
Hope it helps anyone.
希望它可以帮助任何人。