Laravel's Auth Class A bit of a question?

Source: Internet
Author: User

Use Auth;

use Illuminate\Routing\Controller;class AuthController extends Controller {    /**     * Handle an authentication attempt.     *     * @return Response     */    public function authenticate()    {        if (Auth::attempt(['email' => $email, 'password' => $password]))        {            return redirect()->intended('dashboard');        }    }}

How is this auth façade verified? No database query, no code specifically can see AH.

Reply content:

Use Auth;

use Illuminate\Routing\Controller;class AuthController extends Controller {    /**     * Handle an authentication attempt.     *     * @return Response     */    public function authenticate()    {        if (Auth::attempt(['email' => $email, 'password' => $password]))        {            return redirect()->intended('dashboard');        }    }}

How is this auth façade verified? No database query, no code specifically can see AH.

The Auth class was class_alias obtained by renaming the function, which was originally Illuminate\Support\Facades\Auth .

not very encouraging @granton kind of view through Laravel IDE helper, not conducive to learning ( of course, this is a skill, but not very suitable for the new students are learning, it is suitable for the development of the project to quickly locate the source , Because that's what I did.

Go on, if you really want to understand a problem like this in the framework, just walk through the frame, learn fast, and find some new continents. Here are just some of the things that are mentioned in the documentation: Service providers, service containers, façade patterns.

Facade refers to the façade mode, at the beginning I mention the source of the Auth, is a facade, look at the source code, the actual function of the line, if you want to see, is actually a method to return a text string. I suggest you read the documentation about 服务提供者 the section, that place is the core of the build framework function, with the service provider 服务容器 registering a AuthManager provider, when calling facade, facade automatically parse the string returned by that method to generate AuthManager example (strictly speaking, AuthManager is a single case, through its registered Provider). AuthManager provides all the functions of Auth this facade, including automatic (based on configuration) selection of drivers, provided by the driver to you such as attempt, login, check these methods.

If you read through any of the features of the document, especially the Laravel itself, you will find that they support the extension, the way to extend is also to take advantage of the service container, which is the core of the framework. It's easy to extend the way you say and change the way you encrypt.

Read more documents.

2016-07-27 Supplement:

As long as the understanding of the framework of the operating mechanism is easy to understand, in fact, not complex, roughly the following (ignoring the less important details, specifically can read the source code):

    1. Create an Illuminate\Foundation\Application instance;

    2. Create an Http core instance (for Web apps) App\Http\Kernel ---> Illuminate\Foundation\Http\Kernel , arrows indicate inheritance relationships ;

    3. Register the service provider, and perform the registration behavior inside , the authentication component ( Illuminate\Auth\AuthManager ) is Illuminate\Auth\AuthServiceProvider registered in, the registration completes the follow-up operation;

    4. Subsequent services start, such as middleware loading, routing distribution, and response processing, to complete the process.

One of the steps mentioned above 服务提供者(Service Provider) is that Illuminate\Auth\AuthServiceProvider you can see that you are registered in the project configuration config/app.php with a focus:

protected function registerAuthenticator(){    $this->app->singleton('auth', function ($app) {        $app['auth.loaded'] = true;        return new AuthManager($app);    });    $this->app->singleton('auth.driver', function ($app) {        return $app['auth']->guard();    });}

You can see that a singleton named auth is registered, an Illuminate\Auth\AuthManager object, all of our methods that are \Auth accessed through the class are provided by it, and the \Auth class is a class_alias renamed class, and actually access is \Auth equal to access, which Illuminate\Support\Facades\Auth Illuminate\Support\Facades\Auth is a A facade inheritance that provides a way to view the source code:

class Auth extends Facade{    /**     * Get the registered name of the component.     *     * @return string     */    protected static function getFacadeAccessor()    {        return 'auth';    }}

Notice that there return 'auth'; is no, this return value is the Service Provider one registered in the. Why does the facade class have a way of owning the object it points to? In fact, the use of Magic method __callStatic , can be seen through the Illuminate\Support\Facades\Facade source. The documentation has also been mentioned.

As for you to check out AuthManager some of the methods that can be executed, in fact, because AuthManager there are a series of drivers (Driver), which make the Auth components highly customizable, the document on the focus of this piece is described, driven by the two classes, respectively, the implementation of the Illuminate\Contracts\Auth\UserProvider interface and the An instance of the class of the c3/> interface that is used to provide the authentication component with access to the authentication object information, such as the method of obtaining the account and password, the former is your concern, especially retrieveByCredentials with validateCredentials these two interface methods, retrieveByCredentials used to obtain the user instance according to the attempt incoming credentials, c7/> is useful for verifying that the credentials are valid (this is where you want to change the password authentication method).

about how to extend, customize the Auth components, the documentation is explained, so~~~ read the document, this problem read the document, natural solution. It's not a simple look, it's over, so I said anything you know where to look in the documentation, it means you really read the document. All I mentioned above, the documents are written ...

Above.

If you use Ide-helper, you can _ide_helper.php see this code in the

class Auth extends \Illuminate\Support\Facades\Auth{    // ...}

which

/** * Attempt to authenticate a user using the given credentials. * * @param array $credentials * @param bool $remember * @param bool $login * @return bool  * @static  */public static function attempt($credentials = array(), $remember = false, $login = true){    return \Illuminate\Auth\SessionGuard::attempt($credentials, $remember, $login);}

In other words, this attempt method calls the \Illuminate\Auth\SessionGuard::attempt($credentials, $remember, $login) method.

The logic of the specific login verification is inside.

Config in the auth.php configuration of the data model, specifying the model for data query and matching

  • Related Article

    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.