如何在Filament 3+Laravel 10中构建带令牌校验的免登录自定义页面
基于Filament 3实现免登录用户邀请注册页面的最优方案
1. 创建Filament免登录自定义页面
使用Filament Artisan命令生成自定义页面,指定存储路径:
php artisan make:filament-page InvitationRegister --path=pages
修改生成的页面类,隐藏导航并设置免登录访问:
namespace App\Filament\Pages; use Filament\Pages\Page; class InvitationRegister extends Page { protected static ?string $navigationIcon = 'heroicon-o-document-text'; protected static string $view = 'filament.pages.invitation-register'; // 不在Filament后台导航显示 protected static bool $navigation = false; // 允许所有访客访问(免登录) public static function canAccess(): bool { return true; } }
2. 配置邀请路由
在routes/web.php中覆盖Filament默认路由,添加令牌参数:
use App\Filament\Pages\InvitationRegister; Route::get('/invite/{token}', InvitationRegister::class)->name('invitation.register');
邀请链接格式变为/invite/xxx-token-xxx,更贴合业务场景。
3. 页面挂载时校验令牌
在页面类的mount方法中接收令牌参数,校验有效性:
use App\Models\Invitation; use Filament\Notifications\Notification; public ?Invitation $invitation = null; public function mount(string $token): void { $this->invitation = Invitation::where('token', $token) ->where('expires_at', '>', now()) ->whereNull('used_at') ->first(); if (!$this->invitation) { Notification::make() ->title('无效的邀请链接') ->danger() ->send(); redirect()->route('home'); } }
4. 嵌入Filament表单实现注册
修改页面视图resources/views/filament/pages/invitation-register.blade.php,使用Filament原生表单组件构建注册界面:
<x-filament-panels::page> <x-filament-panels::form wire:submit="register"> <x-filament-forms::section title="邀请信息"> <x-filament-forms::field-wrapper label="邀请邮箱"> <div class="text-gray-700">{{ $invitation->email }}</div> </x-filament-forms::field-wrapper> </x-filament-forms::section> <x-filament-forms::section title="账号信息"> <x-filament-forms::input label="用户名" wire:model="name" required /> <x-filament-forms::input label="密码" type="password" wire:model="password" required /> <x-filament-forms::input label="确认密码" type="password" wire:model="password_confirmation" required /> </x-filament-forms::section> <x-filament-panels::form.actions> <x-filament-panels::button type="submit" color="primary"> 完成注册 </x-filament-panels::button> </x-filament-forms::form.actions> </x-filament-panels::form> </x-filament-panels::page>
5. 实现表单提交逻辑
在页面类中添加字段验证和注册逻辑,完成用户创建并标记邀请令牌:
use App\Models\User; use Illuminate\Support\Facades\Hash; public string $name = ''; public string $password = ''; public string $password_confirmation = ''; protected function rules(): array { return [ 'name' => ['required', 'string', 'max:255'], 'password' => ['required', 'string', 'min:8', 'confirmed'], ]; } public function register(): void { $this->validate(); // 创建关联邀请的用户 $user = User::create([ 'name' => $this->name, 'email' => $this->invitation->email, 'password' => Hash::make($this->password), ]); // 标记邀请为已使用 $this->invitation->update(['used_at' => now()]); // 自动登录注册用户 auth()->login($user); Notification::make() ->title('注册成功') ->success() ->send(); redirect()->route('filament.pages.dashboard'); }
方案优势
- 完全基于Filament原生组件,复用样式、验证、通知系统,保持风格统一
- 无需独立编写Livewire组件,代码集中且符合Filament开发规范
- 利用页面生命周期方法处理令牌校验,逻辑清晰易维护
- 路由配置简单,直接绑定Filament页面类,无需额外控制器
内容的提问来源于stack exchange,提问作者larso
相关产品推荐
相关产品推荐

