首页 > 后端开发 > php教程 > 为什么 Laravel 5.2 中未定义'make:auth”命令以及如何手动设置身份验证?

为什么 Laravel 5.2 中未定义'make:auth”命令以及如何手动设置身份验证?

Linda Hamilton
发布: 2024-10-25 08:03:29
原创
916 人浏览过

Why is the

Laravel 身份验证:排除“make:auth 命令未定义”错误

尝试在 Laravel 5.2 中运行 make:auth 命令时,您可能会遇到一条错误消息,指出该命令未定义。出现此问题的原因有多种,我们将详细探讨。

对于 Laravel 5.2 及更早版本,make:auth 命令不可用。 Laravel 5.2 支持以下命令:

  • make:auth
  • make:console
  • make:controller
  • make:entity
  • make:事件
  • make:作业
  • make:监听器
  • make:中间件
  • make:迁移
  • make :model
  • make:policy
  • make:presenter
  • make:provider
  • make:repository
  • make:request
  • make:seeder
  • make:test
  • make:transformer

如果您使用 Laravel 5.2,您可以通过以下方式手动创建身份验证视图和路由请按照以下步骤操作:

  1. 在routes目录中创建routes.php文件。
  2. 将以下代码添加到routes.php文件:

    <code class="php">// Authentication Routes...
    Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
    Route::post('login', 'Auth\LoginController@login')->name('login.post');
    Route::post('logout', 'Auth\LoginController@logout')->name('logout');
    
    // Registration Routes...
    Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
    Route::post('register', 'Auth\RegisterController@register')->name('register.post');
    
    // 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')->name('password.update');</code>
    登录后复制
  3. 在 app/Http/Controllers/Auth 目录下创建 LoginController.php 文件。
  4. 在 LoginController.php 文件中添加以下代码:

    <code class="php">namespace App\Http\Controllers\Auth;
    
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Auth;
    use Illuminate\Support\Facades\Route;
    
    class LoginController extends Controller
    {
        public function showLoginForm()
        {
            return view('auth.login');
        }
    
        public function login(Request $request)
        {
            $credentials = $request->only('email', 'password');
    
            if (Auth::attempt($credentials)) {
                // The user was authenticated
                return redirect()->intended(route('home'));
            }
    
            // The user could not be authenticated
            return redirect()->back()->withErrors(['email' => 'The provided credentials do not match our records.']);
        }
    
        public function logout(Request $request)
        {
            Auth::logout();
    
            return redirect()->route('login');
        }
    }</code>
    登录后复制
  5. 在 app/Http/Controllers/Auth 目录下创建 RegisterController.php 文件。
  6. 在 RegisterController.php 文件中添加以下代码:

    <code class="php">namespace App\Http\Controllers\Auth;
    
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Auth;
    use Illuminate\Support\Facades\Route;
    
    class RegisterController extends Controller
    {
        public function showRegistrationForm()
        {
            return view('auth.register');
        }
    
        public function register(Request $request)
        {
            $this->validate($request, [
                'name' => 'required|string|max:255',
                'email' => 'required|string|email|max:255|unique:users',
                'password' => 'required|string|min:6|confirmed',
            ]);
    
            // Create the user
            $user = User::create([
                'name' => $request->name,
                'email' => $request->email,
                'password' => bcrypt($request->password),
            ]);
    
            // Log the user in
            Auth::login($user);
    
            // Redirect the user to the home page
            return redirect()->route('home');
        }
    }</code>
    登录后复制
  7. 在 app/Http/Controllers/Auth 目录下创建 ForgotPasswordController.php 文件。
  8. 将以下代码添加到 ForgotPasswordController.php 文件中:

    <code class="php">namespace App\Http\Controllers\Auth;
    
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Password;
    
    class ForgotPasswordController extends Controller
    {
        public function showLinkRequestForm()
        {
            return view('auth.passwords.email');
        }
    
        public function sendResetLinkEmail(Request $request)
        {
            $this->validate($request, [
                'email' => 'required|string|email|max:255',
            ]);
    
            // We will send the reset link to this user. Once we have attempted
            // to send the link, we will examine the response then see the message we
            // need to show to the user. Finally, we'll send out a proper
            // response.
            $response = Password::sendResetLink(
                $request->only('email')
            );
    
            switch ($response) {
                case Password::RESET_LINK_SENT:
                    return back()->with('status', __($response));
    
                case Password::INVALID_USER:
                    return back()->withErrors(['email' => __($response)]);
            }
        }
    }</code>
    登录后复制
  9. 在 app/Http/Controllers/Auth 目录下创建 ResetPasswordController.php 文件。
  10. 在 ResetPasswordController.php 文件中添加以下代码:

    <code class="php">namespace App\Http\Controllers\Auth;
    
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Hash;
    use Illuminate\Support\Facades\Password;
    use Illuminate\Validation\ValidationException;
    
    class ResetPasswordController extends Controller
    {
        public function showResetForm(Request $request, $token = null)
        {
            return view('auth.passwords.reset')->with(
                ['token' => $token, 'email' => $request->email]
            );
        }
    
        public function reset(Request $request)
        {
            $request->validate([
                'token' => 'required',
                'email' => 'required|email',
                'password' => 'required|min:6|confirmed',
            ]);
    
            // Here we will attempt to reset the user's password. If it is successful we
            // will update the password on an existing user and return a response
            // indicating that the user's password has been reset.
            $response = Password::reset(
                $request->only('email', 'password', 'password_confirmation', 'token'),
                function ($user) use ($request) {
                    $user->forceFill([
                        'password' => Hash::make($request->password),
                        'remember_token' => Str::random(60),
                    ])->save();
    
                    // In case of large user base, it's recommended to use
                    // $user->setRememberToken(Str::random(60));
                    // $user->save();
                }
            );
    
            switch ($response) {
                case Password::PASSWORD_RESET:
                    return redirect()->route('login')->with('status', __($response));
    
                default:
                    throw ValidationException::withMessages([
                        'email' => [__($response)],
                    ]);
            }
        }
    }</code>
    登录后复制
  11. 打开 resources/views/auth 目录并创建您的视图。

完成这些步骤后,您的 Laravel 5.2 应用程序中应该有一个有效的身份验证系统。

对于 Laravel >= 6

在 Laravel 6 及更高版本中,make:auth 命令已替换为 ui 命令。要使用此命令创建身份验证视图和路由,请运行以下命令:

composer require laravel/ui
php artisan ui vue --auth
php artisan migrate
登录后复制

此命令将安装 Laravel UI 包并创建必要的身份验证视图、路由和迁移。

以上是为什么 Laravel 5.2 中未定义'make:auth”命令以及如何手动设置身份验证?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板