php 如何在 Laravel 中禁用注册新用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29183348/
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
How to disable registration new users in Laravel
提问by Milad Rahimi
I'm using Laravel (v5).
我正在使用 Laravel (v5)。
I need one user and I've already registered that. Now I want to disable registration for new users. Of course, I need the login form to work.
我需要一个用户,而且我已经注册了。现在我想禁用新用户的注册。当然,我需要登录表单才能工作。
How can I do that?
我怎样才能做到这一点?
回答by Limon Monte
Laravel 5.7 introduced the following functionality:
Laravel 5.7 引入了以下功能:
Auth::routes(['register' => false]);
The currently possible options here are:
当前可能的选项是:
Auth::routes([
'register' => false, // Registration Routes...
'reset' => false, // Password Reset Routes...
'verify' => false, // Email Verification Routes...
]);
For older Laravel versions just override showRegistrationForm()
and register()
methods in
对于较旧的 Laravel 版本,只需覆盖showRegistrationForm()
和register()
方法
AuthController
for Laravel 5.0 - 5.4Auth/RegisterController.php
for Laravel 5.5
AuthController
对于 Laravel 5.0 - 5.4Auth/RegisterController.php
适用于 Laravel 5.5
public function showRegistrationForm()
{
return redirect('login');
}
public function register()
{
}
回答by Rafa? G.
If you're using Laravel 5.2 and you installed the auth related functionality with php artisan make:auth
then your app/Http/routes.php
file will include all auth-related routes by simply calling Route::auth()
.
如果您使用的是 Laravel 5.2 并且安装了与身份验证相关的功能,php artisan make:auth
那么您的app/Http/routes.php
文件将包含所有与身份验证相关的路由,只需调用Route::auth()
.
The auth() method can be found in vendor/laravel/framework/src/Illuminate/Routing/Router.php
. So if you want to do as some people suggest here and disable registration by removing unwanted routes (probably a good idea) then you have to copy the routes you still want from the auth() method and put them in app/Http/routes.php
(replacing the call to Route::auth()). So for instance:
auth() 方法可以在vendor/laravel/framework/src/Illuminate/Routing/Router.php
. 因此,如果您想按照某些人的建议进行操作并通过删除不需要的路由(可能是个好主意)来禁用注册,那么您必须从 auth() 方法中复制您仍然需要的路由并将它们放入app/Http/routes.php
(替换对 Route 的调用::认证())。所以例如:
<?php
// This is app/Http/routes.php
// Authentication Routes...
Route::get('login', 'Auth\AuthController@showLoginForm');
Route::post('login', 'Auth\AuthController@login');
Route::get('logout', 'Auth\AuthController@logout');
// Registration Routes... removed!
// Password Reset Routes...
Route::get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
Route::post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
Route::post('password/reset', 'Auth\PasswordController@reset');
If you're using lower version than 5.2 then it's probably different, I remember things changed quite a bit since 5.0, at some point artisan make:auth
was even removed IIRC.
如果您使用的是低于 5.2 的版本,那么它可能会有所不同,我记得自 5.0 以来事情发生了很大变化,在某些时候artisan make:auth
甚至删除了 IIRC。
回答by theeternalsw0rd
This might be new in 5.7, but there is now an options array to the auth method. Simply changing
这可能是 5.7 中的新功能,但现在 auth 方法有一个选项数组。简单地改变
Auth::routes();
to
到
Auth::routes(['register' => false]);
in your routes file after running php artisan make:auth
will disable user registration.
运行后在您的路由文件php artisan make:auth
中将禁用用户注册。
回答by Yassin
For Laravel 5.3 and 5.4, here is the proper way to do it:
对于 Laravel 5.3 和 5.4,以下是正确的方法:
You have to change:
你必须改变:
public function __construct()
{
$this->middleware('guest');
}
to
到
public function __construct()
{
$this->middleware('auth');
}
in app/Http/Controller/Auth/RegisterController.php
在app/Http/Controller/Auth/RegisterController.php
回答by Christopher Geary
As of Laravel 5.7 you can pass an array of options to Auth::routes()
. You can then disable the register routes with:
从 Laravel 5.7 开始,您可以将一组选项传递给Auth::routes()
. 然后,您可以禁用注册路由:
Auth::routes(['register' => false]);
You can see how this works from the source code: src/Illuminate/Routing/Router.php.
你可以从源代码看到它是如何工作的:src/Illuminate/Routing/Router.php。
回答by Phanith Kung
Method 1 for version 5.3
5.3版本方法一
In laravel 5.3 don't have AuthController.
to disable register route you should change in constructor of RegisterController
like this:
在 laravel 5.3 中没有 AuthController。要禁用注册路由,您应该在构造函数中进行RegisterController
如下更改:
You can change form:
你可以改变形式:
public function __construct()
{
$this->middleware('guest');
}
to:
到:
use Illuminate\Support\Facades\Redirect;
public function __construct()
{
Redirect::to('/')->send();
}
Note: for use Redirect
don't forget to user Redirect;
So user access to https://host_name/registerit's redirect to "/".
注意:使用时Redirect
不要忘记 user Redirect;
所以用户访问https://host_name/register是重定向到“/”。
Method 2 for version 5.3
5.3版本方法二
When we use php artisan make:auth
it's added Auth::route();
automatically.
Please Override Route in /routes/web.php.
You can change it's like this:
* you need to comment this line: Auth::routes();
当我们使用php artisan make:auth
它时会Auth::route();
自动添加。请覆盖 /routes/web.php 中的路由。你可以像这样改变它: * 你需要注释这一行:Auth::routes();
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of the routes that are handled
| by your application. Just tell Laravel the URIs it should respond
| to using a Closure or controller method. Build something great!
|
*/
// Auth::routes();
Route::get('/login', 'Auth\LoginController@showLoginForm' );
Route::post('/login', 'Auth\LoginController@login');
Route::post('/logout', 'Auth\LoginController@logout');
Route::get('/home', 'HomeController@index');
Thanks! I hope it's can solve your problems.
谢谢!我希望它可以解决你的问题。
回答by JCoolinger
Overwriting the getRegister and postRegister is tricky - if you are using git there is a high possibility that .gitignore
is set to ignore framework files which will lead to the outcome that registration will still be possible in your production environment (if laravel is installed via composer for example)
覆盖 getRegister 和 postRegister 很棘手 - 如果您正在使用 git,则很有可能.gitignore
设置为忽略框架文件,这将导致在您的生产环境中仍然可以注册的结果(如果 laravel 是通过 Composer 安装的,例如)
Another possibility is using routes.php and adding this line:
另一种可能性是使用 routes.php 并添加以下行:
Route::any('/auth/register','HomeController@index');
This way the framework files are left alone and any request will still be redirected away from the Frameworks register module.
这样,框架文件就不会被处理,任何请求仍将被重定向到框架注册模块之外。
回答by Jesús Amieiro
The AuthController.php
@limonte has overridden is in App\Http\Controllers\Auth
, not in the vendor directory, so Git doesn't ignore this change.
该AuthController.php
@limonte重写了是App\Http\Controllers\Auth
不是在供应商目录,所以Git不会忽略这种变化。
I have added this functions:
我已经添加了这个功能:
public function register() {
return redirect('/');
}
public function showRegistrationForm() {
return redirect('/');
}
and it works correctly.
它工作正常。
回答by Isaac Limón
LAravel 5.6
Laravel 5.6
Auth::routes([
'register' => false, // Registration Routes...
'reset' => false, // Password Reset Routes...
'verify' => false, // Email Verification Routes...
]);
回答by kjdion84
Heres my solution as of 5.4:
这是我从 5.4 开始的解决方案:
//Auth::routes();
// 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');
Notice I've commented out Auth::routes()
and the two registration routes.
请注意,我已经注释掉了Auth::routes()
两条注册路线。
Important:you must also make sure you remove all instances of route('register')
in your app.blade
layout, or Laravel will throw an error.
重要提示:您还必须确保您删除的所有实例route('register')
中的app.blade
布局,或Laravel将抛出一个错误。