Appearance
How to disable registration new users in Laravel
To disable new user registration in Laravel, you can remove the registration routes and controllers from your application. The approach depends on whether you are using modern Laravel scaffolding (like Breeze or Jetstream) or legacy authentication routes.
Method 1: Remove Registration Routes and Controllers
For Laravel Breeze/Jetstream (Laravel 9+):
- Open
routes/auth.phpand comment out or remove the registration-related routes (/register,/register/password, etc.). - Optionally, delete
app/Http/Controllers/Auth/RegisteredUserController.phpand remove its import fromroutes/auth.php. For Legacy Applications: - Open
routes/web.phpand remove the registration routes (e.g.,Auth::routes(['register' => false]);or customRoute::get('/register', ...)). - Remove or comment out the registration controller methods and views if they are no longer needed.
Method 2: Use Custom Middleware
Another approach is to intercept registration requests using middleware. Note that this only blocks routes where the middleware is applied, not the entire application globally.
php
// app/Http/Middleware/DisableRegistration.php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class DisableRegistration
{
public function handle(Request $request, Closure $next): Response
{
if (config('auth.disable_registration', false)) {
return redirect()->route('home')->with('error', 'Registration is currently disabled.');
}
return $next($request);
}
}- Create the middleware using
php artisan make:middleware DisableRegistration. - Register this middleware in your application's middleware configuration (e.g.,
app/Http/Kernel.phpfor Laravel 10 orbootstrap/app.phpfor Laravel 11+) and apply it to your registration routes. - Add
'disable_registration' => false,to yourconfig/auth.phpfile to control the feature toggle.
Method 3: Use Laravel Gates
You can also use Laravel's Gate to control access to the registration controller. This only affects controllers or routes explicitly protected by the gate.
php
// app/Providers/AuthServiceProvider.php
namespace App\Providers;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
// 'App\Models\Model' => 'App\Policies\ModelPolicy',
];
public function boot(): void
{
Gate::define('register', function () {
return !config('auth.disable_registration', false);
});
}
}- Define the gate in
app/Providers/AuthServiceProvider.php. - Protect your registration controller with
$this->middleware('can:register');(Laravel 10 and below) or use route middlewareRoute::middleware('can:register')->group(...). - Ensure the
disable_registrationkey is set inconfig/auth.php.
Choose the method that best fits your application's architecture. Removing routes is the most permanent solution, while middleware and gates allow for dynamic toggling without code changes.