当我学习使用make:auth
的Laravel(5.6)内置身份验证并深入研究生成的路由时,我注意到了Laravel的路由命名。
首先,我发现这些是使用make: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');
第一个路由,名为GET
的{{1}}路由,将调用login
,从而呈现Auth\LoginController@showLoginForm
视图。
第二条路由,即无名auth.login
路由,将调用POST
,该路由从表单中检索提交的数据并验证用户身份。
但是当我查看生成的Auth\LoginController@login
视图的形式时,其动作是对路由auth.login
的{{1}}请求。
POST
AFAIK,此表单将调用login
而不是
<form method="POST" action="{{ route('login') }}" aria-label="{{ __('Login') }}">
基于表单Auth\LoginController@showLoginForm
(即Auth\LoginController@login
)上定义的路由名称。但是,使用表单时,它将正确调用action
。这怎么可能?
答案 0 :(得分:0)
route
助手会生成URL。这两条路由的URI(路径)与刚刚为不同的HTTP方法注册的完全相同的URI /路径。
因此,当您使用该方法时,将获得一个指向http://yoursite/login
的URL。这里没有HTTP方法的含义。这取决于请求来决定使用哪种方法。该URL就是URL,无论如何。
请求进入并匹配URI,该请求还将具有HTTP方法。如果它具有HTTP方法GET,它将命中get
路由;如果它具有HTTP方法POST,则它将命中定义的post
路由。
URI与控制器方法不匹配。在这种情况下,URI和HTTP方法共同决定。