1.路由配置拦截(文件路径:routes/web.php)
Route::group(['middleware' => 'global-auth'], function () {
// 公共首页
Route::get('/', 'HomeController@home')->name('home');
});
2.拦截代码(文件路径:app/Http/Middleware/GlobalAuth.php)
<?php
namespace App\Http\Middleware;
use Illuminate\Support\Facades\Auth;
class GlobalAuth
{
/**
* 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)
{
//登录拦截跳转的方式(这里用redirect重定向方式比较合理)
if (Auth::guest()) {
return redirect(wzRoute('login'));
}
//如果被禁用了,返回无权限页面(这里用abort比较合理,不会改变页面url,用redirect会改变url不合适)
if (\Auth::user() && !empty(\Auth::user()->isDisabled)) {
return abort(403,'system_no_permission');
}
return $next($request);
}
}