Laravel 5.4 设置除注销路由之外的所有身份验证路由 - 使用自定义注销路由/控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45991281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.4 Set All Authentication Routes Except Logout Route - Using Custom Logout Route/Controller
提问by J. Robinson
So for some reason I thought I came across the answer to this question before, but for the life of me I can't find the answer again, either through Google or StackOverflow. This might just be a rubber duck question and I'm sorry if it is, but I hope this question will be of some use to someone someday with this same issue.
因此,出于某种原因,我认为我以前遇到过这个问题的答案,但对于我的生活,我无法通过 Google 或 StackOverflow 再次找到答案。这可能只是一个橡皮鸭问题,如果是,我很抱歉,但我希望这个问题有一天对遇到同样问题的人有用。
Let's assume we're starting with a fresh installation of Laravel 5.4. I run the php artisan make:auth
command in my terminal and it sets up the Authentication Scaffolding for me. Now in my /routes/web.php file I see the following line:
假设我们从全新安装 Laravel 5.4 开始。我php artisan make:auth
在终端中运行该命令,它为我设置了身份验证脚手架。现在在我的 /routes/web.php 文件中,我看到以下行:
Auth::routes();
Auth::routes();
Which is awesome and the route list has all the Authentication routes listed including the logout route defined. (Typed php artisan r:l
to double check) Now I want to set a custom logout route for the user using the a custom Logout Controller. Now, I thought that there was a method you could chain onto a group of resource routes called 'except()' but for the life of me I can't find any information in the documentation about this method. I don't know if this method even exists let alone know what to pass it.
这很棒,路由列表列出了所有身份验证路由,包括定义的注销路由。(键入php artisan r:l
以仔细检查)现在我想使用自定义注销控制器为用户设置自定义注销路由。现在,我认为有一种方法可以链接到一组名为“except()”的资源路由上,但在我的一生中,我在文档中找不到有关此方法的任何信息。我不知道这个方法是否存在,更不用说知道传递什么了。
I'm assuming that the Auth::routes()
generation can use the except method like in a resource route, but I'm not entirely sure how to implement it?
我假设这Auth::routes()
一代可以像在资源路由中一样使用 except 方法,但我不完全确定如何实现它?
So the question is simple. How do I include all authentication routes except the logout route, and then i'll define the logout route using the following line.
所以问题很简单。如何包含除注销路由之外的所有身份验证路由,然后我将使用以下行定义注销路由。
Route::get('logout', 'LogoutController@userLogout')->name('logout');
Route::get('logout', 'LogoutController@userLogout')->name('logout');
Sorry if this is a duplicate entry, I've used the search bar for the past hour and nothing is answering my question.
抱歉,如果这是重复条目,我在过去一小时内使用了搜索栏,但没有任何内容可以回答我的问题。
Edit: I did more research into Resource Routes and realized that it's not a method that I chain onto the route, but rather an array with a key value pair. See code below (Ripped from laravel docs).
编辑:我对资源路由进行了更多研究,并意识到它不是我链接到路由的方法,而是具有键值对的数组。请参阅下面的代码(从 laravel 文档中提取)。
Route::resource('photo', 'PhotoController', ['except' => [
'create', 'store', 'update', 'destroy'
]]);
However when I pass the array into the routes()
method (see code below) the logout route is still there in the route list. However no error is being thrown upon the php artisan r:l
command.
但是,当我将数组传递给routes()
方法(见下面的代码)时,注销路由仍然在路由列表中。但是,该php artisan r:l
命令没有抛出任何错误。
// User Authentication Routes
Auth::routes(['except' => 'logout']);
EDIT: After much digging around it seems like it is not possible to do this type of functionality. I have submitted an issue to the laravel/framework github repo requesting the feature be added in 5.5.
编辑:经过大量挖掘后,似乎无法执行此类功能。我已经向 laravel/framework github repo 提交了一个问题,要求在 5.5 中添加该功能。
Exclude route from Laravel authentication
回答by jacurtis
Robinson, thanks for reaching out to me on Twitterfor help on this answer. I actually think this is a great question and hope this question finds its way up Google ranks for future devs to find and maybe spark a little discussion in the community.
Robinson,感谢您在 Twitter 上与我联系以寻求有关此答案的帮助。我实际上认为这是一个很好的问题,并希望这个问题能在谷歌排名中找到,供未来的开发人员找到并在社区中引发一些讨论。
How can I prevent the logout route from being produced with Auth::routes()
command?
如何防止使用Auth::routes()
命令生成注销路由?
Long story short... you can't.If you want to use the Auth::routes()
shorthand, then it is an all-or-nothingtype of command. You can view the actual source code behind that command here for Laravel v5.5(although it hasnt changed since version 5.2 I believe), and viewing the actual code behind the shorthand command you can see that it accepts no parameters such as the ['except' => 'logout']
array that you have come to expect with other route helper commands in Laravel. There are no hidden, undocumented secrets here to make this command do anything unique, by inspecting the source code we can see that it isn't setup to do anything other than return a pre-set list of routes.
长话短说……你不能。如果您想使用Auth::routes()
速记,则它是一种全有或全无类型的命令。您可以查看该命令这里Laravel V5.5背后实际的源代码(虽然它未自5.2我相信版本改变),并查看实际代码的缩写命令后面你可以看到,它不接受任何参数,如['except' => 'logout']
数组你已经开始期待 Laravel 中的其他路由辅助命令。这里没有隐藏的、未记录的秘密来使这个命令做任何独特的事情,通过检查源代码我们可以看到它没有设置为除了返回预设的路由列表之外做任何事情。
It literally just outputs a set of routes with no logic (decision making) at all. This command is just a shorthand method that prevents you from having to write all those routes yourself.
它实际上只是输出一组完全没有逻辑(决策)的路由。此命令只是一种速记方法,可防止您自己编写所有这些路由。
With that being said, there is no reason that you HAVE to use this shorthand. You can manually make all of these routes yourself. Look at the source code I linked above or run php artisan r:l
(another shorthand for route:list
for any noobs reading this) and copy the same output into your routes file. This allows you to skip over or modify any routes to your liking.
话虽如此,您没有理由必须使用这种速记。您可以自己手动制作所有这些路线。查看我上面链接的源代码或运行php artisan r:l
(route:list
任何新手阅读此内容的另一种速记)并将相同的输出复制到您的路由文件中。这允许您根据自己的喜好跳过或修改任何路线。
This is what the routes would look like if you want to add them without the shorthand command. Make sure to delete that command and paste these in instead. Whichever commands you want to delete or modify can easily be done so like any other route. I took this from the Laravel source code, so it is 100% EXACTLYthe same as the shorthand command creates, including the order of the routes (and technically the comments too).
如果您想在没有速记命令的情况下添加路由,这就是路由的样子。确保删除该命令并粘贴这些命令。您想要删除或修改的任何命令都可以像任何其他路由一样轻松完成。我把这个从Laravel源代码,所以它是100%EXACTLY一样的速记命令创建,包括路由(和技术上的评论太)的顺序。
// Authentication Routes...
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('register', 'Auth\RegisterController@register');
// Password Reset Routes...
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');
So if you wanted to copy this code block and add it to your routes file, it would give you the exact same results (and does the same thing behind the scenes) as the shorthand Auth::routes()
command does. Now you can customize or remove any of the commands that you want.
因此,如果您想复制此代码块并将其添加到您的路由文件中,它将为您提供与速记Auth::routes()
命令完全相同的结果(并在幕后做同样的事情)。现在您可以自定义或删除您想要的任何命令。
How do I change the logout route into a GET
route?
如何将注销路由更改为GET
路由?
So now that we discussed how you work with authentication scaffolding routes, lets answer the meat and potatoes behind your question. You are looking to make the logout command accessible via a GET
request (as opposed to a POST
request which is how Laravel sets it up by default.
既然我们讨论了如何使用身份验证脚手架路由,让我们回答问题背后的主要内容。您希望通过GET
请求(而不是POST
Laravel 默认设置它的请求)访问注销命令。
Easy enough, there are two ways to do this:
很简单,有两种方法可以做到这一点:
1) Copy all the routes and change the logout route to a GET
request
1)复制所有路由,将注销路由改为GET
请求
This is easy enough, delete the Auth::routes()
shorthand and then copy the routes I posted above and paste it into your routes file in the same location that your Auth::routes()
used to be. Then on the third route of our new authentication routes, we find the logout one. Simply change it from Route::post()
to Route::get()
and leave everything else the same.
这很容易,删除Auth::routes()
速记,然后复制我在上面发布的路线并将其粘贴到您以前所在位置的路线文件中Auth::routes()
。然后在我们新的身份验证路由的第三条路由上,我们找到了注销的路由。只需将其从 更改Route::post()
为Route::get()
并保持其他所有内容不变。
Your routes file will look like this now: You could just copy and paste this below and go about your business now.
您的路线文件现在将如下所示:您只需将其复制并粘贴到下方即可开始您的业务。
// Authentication Routes...
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::get('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('register', 'Auth\RegisterController@register');
// Password Reset Routes...
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');
2) The second option is to add a GET
version in addition to your Auth::routes()
shorthand.
2)第二个选项是GET
在您的Auth::routes()
速记之外添加一个版本。
This would ultimately make the logout route accessible via both a GET
and a POST
request. They both link to the same method, so they do the same thing and work in the same way and now you can logout as either a POST
or GET
request.
这最终将使注销路由可通过 aGET
和POST
请求访问。它们都链接到相同的方法,因此它们执行相同的操作并以相同的方式工作,现在您可以作为 aPOST
或GET
request注销。
The resulting code of this option would be this:
此选项的结果代码如下:
Auth::routes();
Route::get('logout', 'Auth\LoginController@logout');
Note that I omitted the name from the second line. It is already named thanks to the Auth::routes()
command. So you don't need to name it again. The logout
name will still work though. So you can still use <a href="{{ route('logout') }}">Logout</a>
in your blade files and the route name will work regardless of whether you are using it as a GET
or POST
request.
请注意,我省略了第二行中的名称。由于Auth::routes()
命令,它已经命名。所以你不需要再次命名它。不过这个logout
名字仍然有效。所以你仍然可以<a href="{{ route('logout') }}">Logout</a>
在你的刀片文件中使用路由名称,无论你是将它用作 aGET
还是POST
request。
So there you go, a long winding journey into the world of Auth::routes()
shorthand command.
所以你去了,进入Auth::routes()
速记命令世界的漫长而曲折的旅程。
回答by J. Robinson
In Laravel 6.x, some Authentication Routes have changed. You can use the following line of code to exclude the register route.
在 Laravel 6.x 中,一些身份验证路由发生了变化。您可以使用以下代码行来排除注册路由。
Auth::routes(['register' => false]);
Auth::routes(['register' => false]);
However, since you're requiring that the register route still remains intact, you would have to do what the other person that answered this question has done, and define all the routes manually. However, their answer hasn't been updated to the current version of Laravel. So here is an updated list of all the routes as of version 6.x.
但是,由于您要求注册路由仍保持不变,因此您必须执行回答此问题的其他人所做的操作,并手动定义所有路由。但是,他们的答案尚未更新到 Laravel 的当前版本。所以这里是版本 6.x 中所有路由的更新列表。
// User Authentication Routes
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
// User Registration Routes
Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('register', 'Auth\RegisterController@register');
// User Password Reset Routes
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset')->name('password.update');
// User Verification Routes
Route::get('email/verify', 'Auth\VerificationController@show')->name('verification.notice');
Route::get('email/verify/{id}/{hash}', 'Auth\VerificationController@verify')->name('verification.verify');
Route::post('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');