从 Laravel 身份验证中排除路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35132245/
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
Exclude route from Laravel authentication
提问by user2810895
After running php artisan make:auth
all the required routes are in the route.php
file, but is it possible to remove one (I want to remove the register route)?
运行php artisan make:auth
完所有必需的路由都在route.php
文件中后,是否可以删除一个(我想删除注册路由)?
Currently I have
目前我有
Route::group(['middleware' => 'web'], function () {
Route::auth();
});
I know that Route::auth()
is a shortcut to add all the routes.
Should I specify the routes myself instead of using the shortcut?
我知道这Route::auth()
是添加所有路线的快捷方式。我应该自己指定路线而不是使用快捷方式吗?
采纳答案by Mark Davidson
Unfortunately you can't exclude register with the current implementation of Route::auth()
.
不幸的是,您不能排除 register 的当前实现Route::auth()
。
You would have to specify all the routes manually so
您必须手动指定所有路线,以便
// Authentication Routes...
$this->get('login', 'Auth\AuthController@showLoginForm');
$this->post('login', 'Auth\AuthController@login');
$this->get('logout', 'Auth\AuthController@logout');
// Password Reset Routes...
$this->get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
$this->post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
$this->post('password/reset', 'Auth\PasswordController@reset');
I think this is a fairly common thing to want to do it would be nice if there was a parameter to the auth
method to say without register maybe you could submit a pull-request to the project.
我认为这是一件相当普遍的事情,如果该auth
方法有一个参数可以在没有注册的情况下说也许你可以向项目提交拉取请求,那就太好了。
回答by Brainmaniac
I just YOLO and change this in the RegisterController.php
我只是 YOLO 并在 RegisterController.php 中更改它
public function __construct()
{
$this->middleware('guest');
}
to this
对此
public function __construct()
{
$this->middleware('auth');
}
This makes the register page require you to be loged in to reach it.
这使得注册页面需要您登录才能访问它。
It's a hack. But it is a good hack.
这是一个黑客。但这是一个很好的黑客。
EDIT: and just add this to your seeder to make life easier:
编辑:并将其添加到您的播种机中,让生活更轻松:
$u1= new App\User;
$u1->name = 'Your name';
$u1->email = '[email protected]';
$u1->password = bcrypt('yourPassword');
$u1->save();
回答by Enis P. Agini?
In the new releases you can do it like so (in your web.php file):
在新版本中,您可以这样做(在您的 web.php 文件中):
Auth::routes(['register'=>false]);
回答by camelCase
As Mark Davidson said, it's not possible out of the box. But this is how I have handled.
正如马克戴维森所说,开箱即用是不可能的。但这就是我的处理方式。
Now it might be overkill, but I pass along an array of what is needed. If no parameters are passed, then default routes are created.
现在它可能有点矫枉过正,但我传递了一系列需要的东西。如果没有传递参数,则创建默认路由。
// Include the authentication and password routes
Route::auth(['authentication', 'password']);
/**
* Register the typical authentication routes for an application.
*
* @param array $options
* @return void
*/
public function auth(array $options = [])
{
if ($options) {
// Authentication Routes...
if (in_array('authentication', $options)) {
$this->get('login', 'Auth\AuthController@showLoginForm');
$this->post('login', 'Auth\AuthController@login');
$this->get('logout', 'Auth\AuthController@logout');
}
// Registration Routes...
if (in_array('registration', $options)) {
$this->get('register', 'Auth\AuthController@showRegistrationForm');
$this->post('register', 'Auth\AuthController@register');
}
// Password Reset Routes...
if (in_array('password', $options)) {
$this->get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
$this->post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
$this->post('password/reset', 'Auth\PasswordController@reset');
}
} else {
// Authentication Routes...
$this->get('login', 'Auth\AuthController@showLoginForm');
$this->post('login', 'Auth\AuthController@login');
$this->get('logout', 'Auth\AuthController@logout');
// Registration Routes...
$this->get('register', 'Auth\AuthController@showRegistrationForm');
$this->post('register', 'Auth\AuthController@register');
// Password Reset Routes...
$this->get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
$this->post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
$this->post('password/reset', 'Auth\PasswordController@reset');
}
}
For your case, you can probably just pass a boolean
as parameter instead of an array
. If the boolean is true
then do not load the register
routes, otherwise load everything.
对于您的情况,您可能只传递一个boolean
as 参数而不是array
. 如果布尔值为true
则不加载register
路由,否则加载所有内容。
Hope it helps.
希望能帮助到你。
回答by Hendrik Jan
You can add a redirect to the /register route like so:
您可以像这样向 /register 路由添加重定向:
<?php
Auth::routes();
// prevent registration, this needs to be after Auth::routes()
Route::redirect('register', '/home', 301);