简体   繁体   English

在 Laravel 8 中禁用 Auth Register 路由?

[英]Disable Auth Register route in Laravel 8?

I need to disable my register route in Laravel 8. Tried我需要在 Laravel 8 中禁用我的注册路由。试过了

Auth::routes([
     'register' => false,
     'login' => false,
]);

but the application threw up an error.但是应用程序抛出了一个错误。

RuntimeException
In order to use the Auth::routes() method, please install the laravel/ui package.

If anyone points out what needs to change, will be grateful.如果有人指出需要更改的内容,将不胜感激。

Thanks谢谢

Laravel 8 uses fortify authentication. Laravel 8 使用强化身份验证。 To disable registration from your laravel app, you need to disable it from fortify , which is located on /config/fortify.php要从您的 laravel 应用程序禁用注册,您需要从位于/config/fortify.phpfortify禁用它

'features' => [
    // Features::registration(), // disable here
     Features::resetPasswords(),
     Features::emailVerification(),
     Features::updateProfileInformation(),
     Features::updatePasswords(),
     Features::twoFactorAuthentication(),
],

At the end of my routes/web.php was the following line:在我的routes/web.php是以下行:

require __DIR__.'/auth.php';

In routes/auth.php are listed all additional routes for user login/register/logout.routes/auth.php中列出了用户登录/注册/注销的所有附加路由。 Just comment out or remove /register route from there.只需从那里注释掉或删除/register路由。

In addition, be sure to disable related routes, in routes/web.php :另外,一定要禁用相关路由,在routes/web.php

Route::get('/register', function() {
    return redirect('/login');
});

Route::post('/register', function() {
    return redirect('/login');
});

I changed according feature tests in tests/Feature/RegistrationTest.php to try to keep work clean so I needed those redirections.我根据tests/Feature/RegistrationTest.php中的功能测试进行了更改,以尝试保持工作清洁,所以我需要这些重定向。

Remove the registration routes from config/auth.php and then create a config/fortify.php (paste the content from: vendor/laravel/fortify/config/fortify.php ) which will override the default settings.config/auth.php中删除注册路由,然后创建一个config/fortify.php (从以下位置粘贴内容: vendor/laravel/fortify/config/fortify.php ),它将覆盖默认设置。

Inside config/fortify.php comment out the first element of features array ( Features::registration() ) then run php artisan optimize to clear config cache and routes cache.config/fortify.php 中注释掉 features 数组的第一个元素 ( Features::registration() ) 然后运行​​php artisan optimize以清除配置缓存和路由缓存。

Now all your removed routes should return 404, you can also check if those still exist with php artisan route:list现在所有已删除的路由都应返回 404,您还可以使用php artisan route:list检查这些路由是否仍然存在

config/fortify.php:配置/fortify.php:

<?php

use Laravel\Fortify\Features;

return [
    'guard' => 'web',
    'middleware' => ['web'],
    'passwords' => 'users',
    'username' => 'email',
    'email' => 'email',
    'views' => true,
    'home' => '/home',
    'prefix' => '',
    'domain' => null,
    'limiters' => [
        'login' => null,
    ],
    'features' => [
        //Features::registration(),
        Features::resetPasswords(),
        Features::emailVerification(),
        Features::updateProfileInformation(),
        Features::updatePasswords(),
        Features::twoFactorAuthentication(),
    ],
];

只需使用:

Auth::routes(['register' => false]);

Do not break your head with different versions of packages and Laravel.不要用不同版本的包和 Laravel 打破你的头脑。 Because maybe you don't have fortify.php in your config, or using different packages.因为也许你的配置中没有 fortify.php,或者使用不同的包。 All routes are in routes/web now.所有路线现在都在 routes/web 中。 Just go there and force that '/register' sends to login or any other view you want to:只需去那里并强制“/注册”发送到登录或您想要的任何其他视图:

Route::any('/register', function() {
    return  view('auth.login');
});

That way you maintain out of reach that feature, but close for when you need it.这样,您就可以保持该功能遥不可及,但在需要时关闭。

Remove this code from routes/auth.php从 routes/auth.php 中删除此代码

Route::get('/register', [RegisteredUserController::class, 'create'])
                ->middleware('guest')
                ->name('register');

Route::post('/register', [RegisteredUserController::class, 'store'])
                ->middleware('guest');

只需将其放在您的/routes/web.php中:

Route::any('/register', [App\Http\Controllers\HomeController::class, 'index']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM