Laravel5.1-phplaravel how to elegantly write a login authentication middleware fish route

Source: Internet
Author: User
I have read laravel's auth LOGIN middleware. Currently, the user table corresponding to auth has been used. Now I need to create a persistence table Operator; this is the login table model of a front-end operator {code ...} to access this page, OperatorAuth must be verified in the middle... read laravel's auth LOGIN middleware;

Currently, the user table corresponding to auth is used;

Now you need to create a keep tableOperatorThis is the login table model of a front-end operator.

Route::get('operator/index_1.html', ['middleware' => 'OperatorAuth', OperatorController@indexOne);Route::get('operator/index_2.html', ['middleware' => 'OperatorAuth', OperatorController@indexTwo);

To access this page, perform OperatorAuth intermediate verification.

There are two operators who access the preceding two routes

OperatorAuthHow do I write this middleware? Do I need to continue laravel auth?

Also, let's look at the process;

Visitor accessoperator/index_1.htmlMiddlewareOperatorAuthLog On. return after Successful Logonoperator/index_1.html

Visitor accessoperator/index_2.htmlMiddlewareOperatorAuthLog On. return after Successful Logonoperator/index_2.html

After the middleware verification is successful, the access address is returned. How can this be written in the middleware?

Is there any elegant code written by laravel? I saw a lot of code written by laravel very beautiful. The code I wrote is ugly.

Please give me some guidance. I have a low IQ.

Reply content:

Read laravel's auth LOGIN middleware;

Currently, the user table corresponding to auth is used;

Now you need to create a keep tableOperatorThis is the login table model of a front-end operator.

Route::get('operator/index_1.html', ['middleware' => 'OperatorAuth', OperatorController@indexOne);Route::get('operator/index_2.html', ['middleware' => 'OperatorAuth', OperatorController@indexTwo);

To access this page, perform OperatorAuth intermediate verification.

There are two operators who access the preceding two routes

OperatorAuthHow do I write this middleware? Do I need to continue laravel auth?

Also, let's look at the process;

Visitor accessoperator/index_1.htmlMiddlewareOperatorAuthLog On. return after Successful Logonoperator/index_1.html

Visitor accessoperator/index_2.htmlMiddlewareOperatorAuthLog On. return after Successful Logonoperator/index_2.html

After the middleware verification is successful, the access address is returned. How can this be written in the middleware?

Is there any elegant code written by laravel? I saw a lot of code written by laravel very beautiful. The code I wrote is ugly.

Please give me some guidance. I have a low IQ.

First, the command line:

php artisan make:middleware FrontEndOperatorAuth

Second, findapp/Http/MiddlewaresFolderFrontEndOperatorAuth.php, Inhandle()Method to write your logic:

  public function handle($request, Closure $next)    {        $user = $request->user();        if ( $user && $user->isOperator() ) {            return $next($request);        }        return redirect()->back();    }    

ThirdUser.phpAddisOperator():

public function isOperator(){       // your logic goes here}

Btw. You want to modify the typos.

Then I recently launched a laravel community: Laravist. If you have any questions about laravel, please come and ask me.

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.