如何在Laravel 5中禁用注册新用户
我试图让Laravel 5中的单用户pipe理面板(只有一个用户),我注册了该用户,所以现在我想禁用新用户注册,当然login表单必须工作,但没有新的注册从今起。 我怎样才能做到这一点?
我使用默认的用户login和版本5注册。
在showRegistrationForm()
重写showRegistrationForm()
和register()
方法:
public function showRegistrationForm() { return redirect('login'); } public function register() { }
如果您使用的是Laravel 5.2,并且您使用php artisan make:auth
安装了auth相关function,那么您的app/Http/routes.php
文件将通过简单地调用Route::auth()
来包含所有与auth相关的路由。
auth()方法可以在vendor/laravel/framework/src/Illuminate/Routing/Router.php
。 所以如果你想按照一些人的build议,通过删除不需要的路由来禁用注册(可能是一个好主意),那么你必须从auth()方法中复制仍然需要的路由,并将它们放在app/Http/routes.php
(将呼叫replace为Route :: auth())。 举个例子:
<?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');
如果你使用的版本低于5.2,那么它可能是不同的,我记得自5.0版本以来,事情发生了很大的变化,在某种程度上, artisan make:auth
甚至被删除了IIRC。
方法1版本5.3
在laravel 5.3中没有AuthController。 禁用寄存器path你应该改变RegisterController
构造函数,如下所示:
你可以改变forms:
public function __construct() { $this->middleware('guest'); }
至:
public function __construct() { Redirect::to('/')->send(); }
注意:对于使用Redirect
不要忘记user Redirect;
所以用户访问https:// host_name /注册它的redirect到“/”。
方法2版本5.3
当我们使用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');
谢谢! 我希望这可以解决你的问题。
对于Laravel 5.3和5.4,这里是正确的方法:
你必须改变:
public function __construct() { $this->middleware('guest'); }
至
public function __construct() { $this->middleware('auth'); }
在app / Http / Controller / Auth / RegisterController.php中
覆盖getRegister和postRegister是非常棘手的 – 如果你使用git,那么.gitignore
被设置为忽略框架文件的可能性很高,这将导致在你的生产环境中注册仍然是可能的(如果laravel是通过composer安装的例如)
另一种可能是使用routes.php并添加这一行:
Route::any('/auth/register','HomeController@index');
这样,框架文件就被保留,任何请求都将被redirect到Frameworks寄存器模块。
AuthController.php @limonte覆盖在App \ Http \ Controllers \ Auth中,而不是在vendor目录中,所以Git不会忽略这个改变。
我已经添加了这个function:
public function register() { return redirect('/'); } public function showRegistrationForm() { return redirect('/'); }
并能正常工作。
我的解决scheme从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');
注意我已经注释了Auth::routes()
和两个注册path。
重要说明:您还必须确保在app.blade
布局中删除route('register')
所有实例,否则Laravel将引发错误。
以下方法很好:
复制/vendor/laravel/framework/src/Illuminate/Routing/Router.php中的所有路由并粘贴到web.php中,注释掉或删除Auth :: routes()。
然后设置一个条件来启用和禁用从.env注册。 复制视图/错误503.blade.php文件,并创build一个403禁止或任何你喜欢的。
将ALLOW_USER_REGISTRATION =添加到.env并通过将其值设置为true或false来控制用户注册。
现在,您完全可以控制路线,供应商文件保持不变。
web.php
//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... if (env('ALLOW_USER_REGISTRATION', true)) { Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register'); Route::post('register', 'Auth\RegisterController@register'); } else { Route::match(['get','post'], 'register', function () { return view('errors.403'); })->name('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');
这是一些以前的答案,特别是Rafal G.和Daniel Centore的组合。
在routes.php
,只需添加以下内容:
if (!env('ALLOW_REGISTRATION', false)) { Route::any('/register', function() { abort(403); }); }
然后你可以有select地控制你的.env
文件中是否允许注册。
我不得不使用:
public function getRegister() { return redirect('/'); }
使用redirect::到()给了我一个错误:
Class 'App\Http\Controllers\Auth\Redirect' not found
在Laravel 5.4
您可以在方法auth()
中的\Illuminate\Routing\Router
中find所有通过Auth::routes()
注册的\Illuminate\Routing\Router
它看起来像这样:
/** * 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'); }
只要复制你想要/需要的路线,你就好!
在laravel 5.3中,您应该通过将下面的代码包含到app\Http\Controllers\Auth
的RegisterController.php
文件中来覆盖默认的showRegistrationForm()
/** * Show the application registration form. * * @return \Illuminate\Http\Response */ public function showRegistrationForm() { //return view('auth.register'); abort(404); //this will throw a page not found exception }
因为你不想允许注册,所以最好扔404 error
所以入侵者知道他是失去了。 当你准备在你的应用程序注册,取消注释//return view('auth.register');
然后评论abort(404);
仅供参考/////////////////////////// ////
如果您需要为用户,成员,学生,pipe理员等使用多个身份validation创build身份validation,那么我build议您结帐这hesto / multi-auth它的一个真棒包在L5应用程序的无限validation 。
在这篇文章中,您可以阅读关于Auth方法及其相关文件的更多信息。
为了不要太改变代码,只要创build一个中间件来检测请求的url是否是url('register'),然后redirect到404或者做任何事情。
在Laravel 5.5
我试图在Laravel 5.5中完成相同的问题。 而不是在web.php路由文件中使用Auth::routes()
,我只包含login/注销路由:
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login'); Route::post('login', 'Auth\LoginController@login'); Route::post('logout', 'Auth\LoginController@logout')->name('logout');
加
use \Redirect;
在文件的顶部