use Auth;
<code>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'); } } } </p> <p>How is this Auth facade verified? There is no database query and no specific code to look at. . </p> <h2>Reply content:</h2> <p><?php namespace AppHttpControllers;</p> <p>use Auth;</p> <pre class="brush:php;toolbar:false"><code>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'); } } } </p> <p>How is this Auth facade verified? There is no database query and no specific code to look at. . </p> <p class="answer fmt" data-id="1020000006071581"> The </p> <p>Auth class is obtained by renaming the <code>class_alias</code> function from <code>IlluminateSupportFacadesAuth</code>. </p> <blockquote><p><strong>Not very encouraging</strong> @granton The way of viewing through laravel ide helper is not conducive to learning (<strong>Of course, this is a technique, but it is not suitable for newcomers who are learning, but it is suitable for quickly locating sources in project development </strong>, because that’s what I do). </p></blockquote> <p>Continue, if you really want to understand similar problems in the entire framework, just follow the framework. Not only will you learn quickly, but you will also discover some new continents. Here I will only mention the parts mentioned in the document for the problem: service provider, service container, facade pattern. </p> <p>Facade refers to the facade mode. I mentioned the source of Auth at the beginning, which is a Facade. If you look at the source code, there is only one line that actually works. If you are willing to take a look, it is actually a method that returns a text string. I suggest you read the documentation about <code>Service Provider</code>. That place is the core of building framework functions. Use the service provider to register an AuthManager provider in the <code>Service Container</code>. When calling Facade, Facade returns according to that method. The string is automatically parsed to generate an AuthManager instance (strictly speaking, AuthManager is a singleton and can be checked through its registered Provider). AuthManager provides all the functions of the Auth facade, including automatically selecting the driver (according to configuration), and the driver provides you with methods such as attempt, login, and check. </p> <p><strong><em>If you carefully read the documentation of any function, especially laravel's own components, you will find that they support extensions, and the way of extension is to use service containers, which is the core of the framework. It's very simple to extend the method you mentioned and change the encryption method. </em></strong></p> <p>Read more documentation. </p> <h3><strong><em>2016-07-27 Supplement: </em></strong></h3> <p>As long as you understand the operating mechanism of this framework, it is easy to understand. In fact, it is not complicated. It is roughly as follows (ignoring the details that are not very important, you can read the source code for details): </p> <ol> <li><p>Create <code>IlluminateFoundationApplication</code> instance; </p></li> <li><p> (for web applications) Create Http core instance <code>AppHttpKernel</code> ---> <code>IlluminateFoundationHttpKernel</code>, <em>arrow indicates inheritance relationship </em>; </p></li> <li><p><strong>Register the service provider and perform the registration behavior inside</strong>. The authentication component (<code>IlluminateAuthAuthManager</code>) is registered in <code>IlluminateAuthAuthServiceProvider</code>. After the registration is completed, follow-up operations are performed; </p></li> <li><p> Subsequent services are started, such as middleware loading, route distribution, response processing, and the process is completed. </p></li> </ol> <p>The above process mentioned a <code>Service Provider</code>, which is <code>IlluminateAuthAuthServiceProvider</code>. You can see that it is registered in the project configuration <code>config/app.php</code>, and there is an important paragraph in it: </p> <pre class="brush:php;toolbar:false"><code>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(); }); }</code>
You can see that a singleton named auth
is registered, which is an IlluminateAuthAuthManager
object. All the method functions we access through the Auth
class are provided by it, and the Auth
class is a class_alias
Rename the obtained class. In fact, accessing Auth
is equivalent to accessing IlluminateSupportFacadesAuth
. IlluminateSupportFacadesAuth
is an inheritance of Facade and provides a method. View the source code:
<code>class Auth extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'auth'; } }</code>
Notice that return 'auth';
No, this return value is the one registered in Service Provider
. Why can the Facade class have methods on the objects it points to? In fact, it uses the magic method __callStatic
, which can be known by viewing the IlluminateSupportFacadesFacade
source code. The documentation also mentions it.
至于你们去查看 AuthManager
并没发现一些可被执行的方法,实际上是因为 AuthManager
下还有一系列驱动(Driver),这些驱动使得 Auth 组件高度可定制化,文档上对这块做了着重讲解,驱动由两类构成,分别是实现了 Illuminate\Contracts\Auth\UserProvider
接口和 Illuminate\Contracts\Auth\Authenticatable
接口的类的实例,后者是用于提供认证组件获取认证对象信息的接口,比如获取账户、密码的方法,前者则是你们关注的,尤其是 retrieveByCredentials
和 validateCredentials
这两个接口方法,retrieveByCredentials
用于根据 attempt 传入的凭据获取用户实例的,validateCredentials
适用于验证凭据是否有效(想改变密码验证方式的就是通过该处实现)。
关于如何扩展、定制 Auth 组件,文档有说明,so~~~多读文档,这种问题读了文档,自然解决。不是简单看就完了,做到我说到任何一点你都知道在文档哪里去找,才说明你真的是读了文档的。我上面所提的所有,文档都写了。。。
以上。
如果使用 ide-helper, 可以在 _ide_helper.php
中看到这段代码
<code class="php">class Auth extends \Illuminate\Support\Facades\Auth{ // ... }</code>
其中
<code class="php">/** * 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); }</code>
也就是说,这个 attempt
方法调用的是 \Illuminate\Auth\SessionGuard::attempt($credentials, $remember, $login)
方法。
具体的登陆验证的逻辑在里面。
config里的auth.php里配置了数据模型的吧,指定了model进行数据查询和匹配