新建一个全局中间件
php artisan make:middleware MyAuthenticate
复制代码
修改中间件类MyAuthenticate
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Guard;
class MyAuthenticate
{
/**
* The Guard implementation.
*
* @var Guard
*/
protected $auth;
/**
* Create a new middleware instance.
*
* @param Guard $auth
* @return void
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$uid = 132;
if ($this->auth->guest()) {
if ($uid) {
$this->auth->loginUsingId($uid, true);
}
} else {
if (empty($uid)) {
$this->auth->logout();
} else {
if ($this->auth->id() != $uid) {
$this->auth->logout();
$this->auth->loginUsingId($uid, true);
}
}
}
return $next($request);
}
}
复制代码
在Kernel.php
中的数组$middleware
内注册全局中间件
\App\Http\Middleware\MyAuthenticate::class,
复制代码