1. The Administrator logs on to middleware and logs off. The Administrator logs on to middleware.
1. Determine whether the user is logged on based on the session. The index homepage can be accessed only after logon. Otherwise, the login page is returned.
(1) modify a route
Route: group (['ddleware '=> ['web', 'admin. login '], 'prefix' => 'admin', 'namespace' => 'admin'], function () {// register a middleware admin. login // prefix is the route prefix and namespace is the namespace, which saves duplicate values for the following routes. // The routes should be placed in the middleware, otherwise, the session will not be generated. // The logon page cannot be placed in the middleware. Otherwise, the logon page cannot be accessed to Route: get ('index', 'indexcontroller @ Index'); Route :: get ('info', 'indexcontroller @ info ');});
(2) app \ Kernel. php
Add admin. login row
protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'admin.login' => \App\Http\Middleware\Adminlogin::class, ];
(3) Go to the project root directory on the console and create Middleware
Php artisan make: middleware Adminlogin
Modify app \ Http \ Middleware \ Adminlogin. php
<? Phpnamespace App \ Http \ Middleware; use Closure; class Adminlogin {/*** Handle an incoming request. ** @ param \ Illuminate \ Http \ Request $ request * @ param \ Closure $ next * @ return mixed * // if the session ('user') is empty, return to the logon page public function handle ($ request, Closure $ next) {if (! Session ('user') {return redirect ('admin/login');} return $ next ($ request );}}
View Code
Verification:
Go to the LoginController. php and the login method to clear the session.
The session information is cleared when you access the admin/login page.
session(['user'=>null]); return view('admin.login');
At this point: After logging on, you can enter the index page, restart another login page, clear the session, return to the index page, refresh, and jump to the logon page (after testing, comment out a session)
2. Homepage exit
(1) Add a route
Route::get('quit','LoginController@quit');
(2) Add the quit method to LoginController. php
public function quit(){ session(['user'=>null]); return redirect('admin/login');}
(3) Modify index. blade. php and complete the exit button.
<Li> <a href = "{url ('admin/quit') }}"> quit </a> </li>
Verification:
After logging on, the user enters the index page and click exit to jump to the login page. The session is cleared. Accessing the index separately will jump to the login page.