Laravel 11 使用 Laravel Breeze 进行注册与登录
在开始之前呢,需要说明一下。我们需要新一个项目进行接下来的开发。
准备工作
1、创建新项目
$ $ laravel new laravel11-blog-1
2、安装扩展包
$ cd laravel11-blog-1
$ chmod -R 777 storage/
$ composer require laravel/breeze
$ php artisan breeze:install
┌ Which Breeze stack would you like to install? ───────────────┐
│ Blade with Alpine │
└──────────────────────────────────────────────────────────────┘
┌ Would you like dark mode support? ───────────────────────────┐
│ No │
└──────────────────────────────────────────────────────────────┘
┌ Which testing framework do you prefer? ──────────────────────┐
│ PHPUnit │
└──────────────────────────────────────────────────────────────┘
INFO Installing and building Node dependencies.
3、创建模型与迁移文件
$ php artisan make:model Category -m
$ php artisan make:model Post -m
4、编写迁移文件
database/migrations/2024_04_05_212432_create_categories_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name')->comment('分类名');
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('categories');
}
};
database/migrations/2024_04_05_212436_create_posts_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('category_id')->default(0)->index()->comment('分类 ID');
$table->string('title')->index()->comment('文章标题');
$table->text('text')->comment('文章内容');
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('posts');
}
};
5、执行迁移
$ php artisan migrate
6、注册与登录
当我们安装完 breeze
后,登录与注册功能就已经完成了。
我们访问项目就可以看到注册与登录按钮。
了解 Breeze 路由
Breeze 安装完成后,会在 routes/web.php 文件中自动添加如下路由:
<?php
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');
Route::middleware('auth')->group(function () {
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});
require __DIR__.'/auth.php';
第一条路由是针对 /dashboard
URL。它有一个额外的方法 middleware
,参数为 auth
。这意味着只有经过身份验证的已登录用户才能访问该页面。
第二条是一组用于用户个人资料的路由。这个组同样具有 auth
中间件。我们将在后面的课程中更详细地讨论中间件和路由组。
最后一行,Breeze 添加了指向一个独立的 Auth 路由文件的链接。在那个文件中,我们可以看到认证所需的所有路由:登录、注册、登出、忘记密码等。
auth 路由
除了在 web.php 文件中为我们自动添加了路由外,还添加了一个 routes/auth.php
路由文件,这里面的可就多了。
但是呢,分为两组:
// routes/auth.php
Route::middleware('guest')->group(function () {
// ...
});
Route::middleware('auth')->group(function () {
// ...
});
guest
是未登录用户访问的,auth
是需要登录后才可以访问了。
请登录后再评论