1. The Administrator logs on to middleware and logs off. The Administrator logs on to middleware.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.