我建议您制作自己的中间件,在那里您可以用默认方法编写身份验证代码
handle
. 然后,您只需要调用该中间件就可以对任何一个用户进行身份验证。
app/Http/Middleware/CustomAuthentiation.php
然后在那里写下你的逻辑,就像下面这个片段:
class CustomAuthentiation
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
// Write your authentication code here and then in last lines, if all is good, forward the execution ahead. Like :
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
return $next($request);
}
}
然后将其添加到
$routeMiddleware
内核中的数组。php文件位于
app/Http/Kernel.php
这样地:
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'myAuth' => \App\Http\Middleware\CustomAuthentiation::class // Here is your middleware..
];
然后可以将该中间件绑定到
routes/web.php
这样地:
Route::middleware('myAuth')->post('login', 'LoginController@LoginUser');
希望这会有所帮助。