laravel 加中间件的方法 防止直接打开后台
Posted Mark的小试牛刀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel 加中间件的方法 防止直接打开后台相关的知识,希望对你有一定的参考价值。
路由 routes.php
Route::group([‘middleware‘ => [‘web‘,‘admin.login.login‘]], function () { //后台首页路由 Route::get(‘/admin/index/index‘,‘Admin\[email protected]‘); });
kernel.php
protected $routeMiddleware = [ ‘auth‘ => \App\Http\Middleware\Authenticate::class, ‘auth.basic‘ => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, ‘can‘ => \Illuminate\Foundation\Http\Middleware\Authorize::class, ‘guest‘ => \App\Http\Middleware\RedirectIfAuthenticated::class, ‘throttle‘ => \Illuminate\Routing\Middleware\ThrottleRequests::class, ‘admin.login.login‘ => \App\Http\Middleware\AdminLogin::class, ];
artisan 创建中间件 cmd命令行中
php artisan make:middleware AdminLogin
Middleware AdminLogin.php
<?php namespace App\Http\Middleware; use Closure; class AdminLogin { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if(!session(‘user‘)){ return redirect(‘admin/login/login‘); } return $next($request); } }
以上是关于laravel 加中间件的方法 防止直接打开后台的主要内容,如果未能解决你的问题,请参考以下文章