You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lumen Laravel 5.5中php artisan make:auth未定义,如何实现认证?

Hey there! Great question—since Lumen is a stripped-down micro-framework compared to full Laravel, the make:auth command doesn’t exist out of the box. But don’t worry, you can absolutely build authentication manually for your Lumen 5.5 project. Let’s break this down step by step:

Manual Authentication Setup for Lumen 5.5

First, Why No make:auth?

Lumen is built for speed and minimalism, so it omits Laravel’s full-stack auth scaffolding that make:auth generates. But that doesn’t mean you can’t add robust auth—you just have to assemble it yourself (or use a lightweight package, but let’s focus on manual setup first).

Step 1: Enable Core Features

First, make sure your app has Eloquent (ORM) and Facades enabled, since we’ll need them for auth logic. Open bootstrap/app.php and uncomment these lines:

// $app->withFacades();
// $app->withEloquent();

Also uncomment the auth service provider line to enable Lumen’s auth utilities:

// $app->register(App\Providers\AuthServiceProvider::class);

Step 2: Create User Model & Migration

You’ll need a users table and corresponding model to store user data. Generate a migration with:

php artisan make:migration create_users_table

Open the migration file in database/migrations and add essential fields:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

Run the migration to create the table:

php artisan migrate

Now create the User model in app/User.php with auth contracts:

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;

class User extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable;

    protected $fillable = [
        'name', 'email',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
}

Step 3: Add Auth Routes

Add these routes to routes/web.php (use routes/api.php if building an API):

// Show login form (web-only)
$router->get('/login', 'AuthController@showLoginForm');
// Handle login submission
$router->post('/login', 'AuthController@login');
// Handle logout
$router->post('/logout', 'AuthController@logout');
// Show registration form (web-only)
$router->get('/register', 'AuthController@showRegistrationForm');
// Handle registration submission
$router->post('/register', 'AuthController@register');

Step 4: Build the Auth Controller

Generate a controller to handle auth logic:

php artisan make:controller AuthController

Fill it with basic login/register/logout logic (adjust for API use if needed):

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller
{
    public function showLoginForm()
    {
        return view('auth.login'); // You'll need to create this view
    }

    public function login(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required',
        ]);

        if (Auth::attempt($request->only('email', 'password'))) {
            return redirect()->intended('/dashboard'); // Redirect to your protected page
        }

        return back()->withErrors([
            'email' => 'These credentials don’t match our records.',
        ]);
    }

    public function showRegistrationForm()
    {
        return view('auth.register'); // Create this view too
    }

    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',
        ]);

        User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);

        Auth::attempt($request->only('email', 'password'));

        return redirect('/dashboard');
    }

    public function logout(Request $request)
    {
        Auth::logout();
        return redirect('/login');
    }
}

Step 5: Create Auth Views (Web Only)

If building a web app, create login/registration views in resources/views/auth/. Example login.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <h1>Login</h1>
    @if ($errors->any())
        <div>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    <form method="POST" action="/login">
        @csrf
        <div>
            <label>Email:</label>
            <input type="email" name="email" required>
        </div>
        <div>
            <label>Password:</label>
            <input type="password" name="password" required>
        </div>
        <button type="submit">Login</button>
    </form>
</body>
</html>

Create a similar register.blade.php with name and password confirmation fields.

Step 6: Protect Routes

To restrict access to authenticated users, enable the auth middleware in bootstrap/app.php:

// $app->routeMiddleware([
//     'auth' => App\Http\Middleware\Authenticate::class,
// ]);

Then apply it to your protected routes:

$router->group(['middleware' => 'auth'], function () use ($router) {
    $router->get('/dashboard', 'DashboardController@index');
    // Add other protected routes here
});

Alternative: Use a Lightweight Package

If you don’t want to build everything from scratch, look for Lumen 5.5-compatible auth packages (just double-check version compatibility before installing).


内容的提问来源于stack exchange,提问作者Andre Robinson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:24:17