Laravel 自动授权讲解
看到这部分文档,经常看见的一句话就是php artisan make:auth,经常好奇这段代码到底干了什么,现在就来扒一扒。
路由
路由文件中会新加入以下内容:
Auth::routes(); Route::get('/home','HomeController@index')->name('home');
首先先是Auth::route();,这句代码等于以下全部设置(文件位置是\Illuminate\Routing\Router.php):
/** * Register the typical authentication routes for an application. * * @return void */ public function auth() { // Authentication Routes... $this->get('login', 'Auth\LoginController@showLoginForm')->name('login'); $this->post('login', 'Auth\LoginController@login'); $this->post('logout', 'Auth\LoginController@logout')->name('logout'); // Registration Routes... $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register'); $this->post('register', 'Auth\RegisterController@register'); // Password Reset Routes... $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request'); $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email'); $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset'); $this->post('password/reset', 'Auth\ResetPasswordController@reset'); }
这一部分先讲注册,首先,可以看到登录(login)的路由指向的是Auth\LoginController@showLoginForm,这个控制器是app\Http\Auth\LoginController.php,这里贴一下他的代码:
class LoginController extends Controller { /* |-------------------------------------------------------------------------- | Login Controller |-------------------------------------------------------------------------- | | This controller handles authenticating users for the application and | redirecting them to your home screen. The controller uses a trait | to conveniently provide its functionality to your applications. | */ use AuthenticatesUsers; /** * Where to redirect users after login. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } }
而其中并没有设置showLoginForm方法,该方法被保存在trait AuthenticatesUsers中,该方法的代码如下:
public function showLoginForm() { return view('auth.login'); }
就是返回一个视图,下面我们来看该视图:
<form class="form-horizontal" method="POST" action="{{ route('login') }}"> </form>
内容版权声明:除非注明,否则皆为本站原创文章。