This article will share with you how to use the user authentication system in the Laravel5 framework, which is very detailed. we will attach an example and recommend it to our friends. if you need it, you can refer to it. Laravel already has a user authentication system. let's take a look at routes. php. if it is deleted, add the following:
Route::controllers([ 'auth' => 'Auth\AuthController', 'password' => 'Auth\PasswordController']);
You can use php artisan route: list to view details. Access/auth/login in the browser and you can see the login interface. it is best to comment out the google-related items in the default app. blade. php, or else you will go crazy.
You can use register, login, or even forget password.
Actually registering a user fails after submission, but it does not actually fail, but larave automatically jumps to/home and we have deleted this controller. You can use tinker to check whether the user has created the SDK.
Trait is actually used in Auth \ AuthController. what is triat? Well, php only supports single inheritance. with trait added in php5.4, a trait is actually a group of methods, and you can include it in another class. Such as an abstract class. you cannot instantiate it directly.
Reference trait in Auth \ AuthController:
The code is as follows:
Use AuthenticatesAndRegistersUsers;
Let's find him and see how he jumps after registration. It is quite hidden, in the vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesAndregistersUsers. php, wow.
Public function redirectPath () {if (property_exists ($ this, 'redirectpath') {return $ this-> redirectPath;} // if the user sets the redirectTo attribute, this will jump to the property set by the user. Otherwise, go to home return property_exists ($ this, 'redirectto ')? $ This-> redirectTo: '/home ';}
OK. Now we know that you can set the redirectTo attribute to customize the redirection after registration. In Auth \ AuthContotroller, modify:
The code is as follows:
Protected $ redirectTo = 'articles ';
We should first use/auth/logout to ensure that we exit. if something goes wrong, don't be afraid. we don't have the default home page. re-access: auth/register to create a new user. this time it should be OK.
Logout again, and then use login to log on.
Now we can delete the hidden field temporarily set in form_partial, and then modify the controller:
Public function store (Requests \ ArticleRequest $ request) {// you can do this // $ request = $ request-> all (); // $ request ['User _ id'] = Auth: id (); // simpler method $ article = Article :: create ($ request-> all (); // laravel automatically completes the external key association Auth: user ()-> articles ()-> save ($ article ); return redirect ('articles ');}
Add an article and use tinker to view it.
Middleware
Of course, we do not want anyone to post articles, at least for login. We add protection in the controller:
public function create() { if (Auth::guest()) { return redirect('articles'); } return view('articles.create'); }
The above code can work. there is a problem. we need to handle it in every method that requires protection. this is too silly. Fortunately, we have middleware.
The middleware can be understood as a processing pipeline. the middleware can process at a certain time point in the pipeline, which can be a request or a response. Requests may be redirected or passed based on the processing rules of the middleware.
The app/http/middleware contains three middleware components. you can see what the middleware is doing by name. check them carefully. Note that Closure $ next represents the next middleware.
Register the middleware in app/http/kernel. php. $ Middleware declares the middleware that processes all http requests. $ routeMiddleware only processes routes, and the declaration that you must display must use one or more of these middleware.
If we want to protect the entire ArticlesController, we can directly add the middleware to the constructor:
public function __construct() { $this->middleware('auth'); }
Now, all methods are protected.
But we may not want the entire controller to be protected. what if it is only one or two of them? We can handle it like this:
Public function _ construct () {$ this-> middleware ('auth', ['only' => 'create']); // of course, this can be reversed. // $ this-> middleware ('auth', ['couldn't '=> 'index']);}
We do not necessarily introduce middleware in the controller constructor. we can directly declare in the routing:
The code is as follows:
Route: get ('about', ['middleware '=> 'auth', 'uses' => 'pagescontroller @ about']);
In. system Middleware provided in php, such as 'illuminate \ Foundation \ Http \ Middleware \ checkformaintenancemode', enables us to enter the maintenance mode. for example, the system is online, but now we need to temporarily close it for a period of time. we can process it in the command line and take a look at the work of this Middleware:
The code is as follows:
Php artisan down
Visit the website and you can see that any url request is immediately back. Website Launch:
The code is as follows:
Php artisan up
Let's make our own middleware:
The code is as follows:
Php artisan make: middleware Demo
Then add the code:
Public function handle ($ request, Closure $ next) {// if the request contains foo, we will return to the controller homepage if ($ request-> has ('Foo ')) {return redirect ('Articles ');} return $ next ($ request );}
If you want to use middleware in all requests, you need to register in $ middleware in kernel. php:
protected $middleware = [ ... 'App\Http\Middleware\Demo', ];
Now let's test it. suppose we access/articles/create? Foo = bar. we are redirected to the homepage.
Let's remove this display middleware and create a truly useful middleware. Suppose we want to protect a page, which must be accessible by managers.
The code is as follows:
Php artisan make: middleware RedirectIfNotAManager
Let's add the processing code:
public function handle($request, Closure $next) { if (!$request->user() || !$request->user()->isATeamManager()) { return redirect('articles'); } return $next($request); }
Modify our model as follows:
public function isATeamManager() { return false; }
For simplicity, false is returned directly. This time we place the middleware in $ routeMiddleware in kernel. php.
protected $routeMiddleware = [ ... 'manager' => 'App\Http\Middleware\RedirectIfNotAManager', ];
Let's test the route:
Route::get('foo', ['middleware' => 'manager', function() { return 'This page may only be viewed by manager';}]);
The home page is returned for access by guest identity or logon identity. However, if isATeamManager () is modified, true is returned.
The above is all the content described in this article. I hope you will be familiar with the Laravel5 framework.