Laravel with the Auth class and user model to help us to facilitate the implementation of user login, judgment.
First, configure the relevant parameters first app/config/auth.php
:
The reason is ditto.
As you can see, it's easy to use without even configuring it, so let's look at how to use it.
In the future, for example, each visit must first determine whether the user is logged in, in other frameworks we generally used to judge in all the controller's parent controller, that is, in the Laravel app/controllers/BaseController.php
class __construct method:
<?PHPclassBasecontrollerextendsController { Public function__construct () {if(Auth::check () = =false){ returnRedirect::guest (' Login '); } } /** * Setup the layout used by the controller. * * @return void*/ protected functionsetuplayout () {if( !Is_null($this-layout)) { $this->layout = View::make ($this-layout); } }}
The code is easy to understand, we Auth::check()
can determine whether the user login status, if not, directly redirect to /login
the URL, why not use Redirect::guest()
Redirect::to()
it, through the API manual can be found:
Redirect::guest()
When redirected, the current URL is saved to the session so that you can use the Redirect::intended()
method to jump to the previous page to continue the business after logging in.
Jump to /login
This page, of course, you have to write a good route, you can point to a controller method, the details are not mentioned, assuming that the login form submission processing methods are as follows:
Public function Postlogin () { if (auth::attempt (array$email$password))) { return redirect::intended ('/'); }}
Auth::attempt()
Method can be used to verify that user-submitted login information and user
table matching, in the example, password
This field is fixed, you should also have a corresponding field in the user table, and the width of at least 60, remember not MD5. And the email
field is casual, you may be using username as a unique identifier, this varies depending on the project, here is a random email as login account name, the database also has the corresponding fields.
Perhaps some people will be more difficult to understand, in fact, as long as a change of angle, auth just help us to achieve the original need to write the verification logic, remember the first configuration parameters have model and Table,auth is based on this automatic help us to query, if the match will automatically help us write session , so that Auth::check()
the next time will pass.
Redirect::intended(‘/‘)
This method means to jump to the previous page, if using the method as above, Redirect::guest()
then intended here will jump to the URL at that time, and its parameters are just a default value, and no history URL will jump to '/'.
You can also continue to optimize, for example, we should not do auth::check in Basecontroller, we can use Route::filter to verify before the request, which can be referred to in the manual for the relevant section of the route.
Auth also has some other methods, such as the Auth::basic()
implementation of HTTP Basic authentication, detailed reference manual "Authentication" section, as well as related APIs, this article only describes the approximate verification process, will not delve into, after all, the grandmother himself only touch a little content.
Laravel Auth Verification