在Laravel 8中禁用身份验证注册路由?

时间:2020-10-16 05:53:57

标签: php laravel laravel-8

我需要在Laravel 8中禁用注册路由。

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

但是应用程序抛出了一个错误。

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

如果有人指出需要更改的内容,将不胜感激。

谢谢

6 个答案:

答案 0 :(得分:5)

Laravel 8使用 fortify 身份验证。要从laravel应用中禁用注册,您需要从位于/config/fortify.php

上的 fortify 中禁用它
'features' => [
    // Features::registration(), // disable here
     Features::resetPasswords(),
     Features::emailVerification(),
     Features::updateProfileInformation(),
     Features::updatePasswords(),
     Features::twoFactorAuthentication(),
],

答案 1 :(得分:2)

另外,一定要禁用相关路由,在routes/web.php中:

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

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

我根据 tests/Feature/RegistrationTest.php 中的功能测试进行了更改,以尽量保持工作干净,因此我需要这些重定向。

答案 2 :(得分:1)

在我的 routes/web.php 末尾是以下行:

require __DIR__.'/auth.php';

routes/auth.php 中列出了用户登录/注册/注销的所有附加路由。只需注释掉或从那里删除 /register 路线。

答案 3 :(得分:1)

从 routes/auth.php 中删除此代码

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

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

答案 4 :(得分:0)

不要用不同版本的包和 Laravel 伤脑筋。因为也许你的配置中没有 fortify.php,或者使用不同的包。所有路线现在都在路线/网络中。只需去那里并强制'/register'发送到登录或您想要的任何其他视图:

// <project-root>/firebase.json
{
  "react-native": {
    "analytics_auto_collection_enabled": false
  }
}

这样您就可以保持该功能遥不可及,但在需要时关闭。

答案 5 :(得分:0)

config/auth.php 中删除注册路由,然后创建一个 config/fortify.php(从以下位置粘贴内容:vendor/laravel/fortify /config/fortify.php) 将覆盖默认设置。

config/fortify.php 中注释掉 features 数组的第一个元素 (Features::registration()) 然后运行 ​​php artisan optimize 清除配置缓存和路由缓存。

现在所有删除的路由都应该返回 404,您还可以使用 php artisan route:list

检查那些路由是否仍然存在

config/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(),
    ],
];