spatie / laravel-welcome-notification
向新用户发送欢迎通知
Requires
- php: ^7.3|^8.0
- illuminate/auth: ^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/notifications: ^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/queue: ^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^5.0|^6.0|^7.0|^8.0|^9.0
- pestphp/pest: ^1.22|^2.34
- pestphp/pest-plugin-laravel: ^1.3|^2.3
- phpunit/phpunit: ^9.0|^10.5
- spatie/test-time: ^1.1
README
使用此包,您可以为您的应用程序的新用户发送一个 WelcomeNotification
。通知包含一个安全链接,用户可以通过该链接设置初始密码。
$expiresAt = now()->addDay(); $user->sendWelcomeNotification($expiresAt);
支持我们
我们投入了大量资源来创建最优质的开放式软件包。您可以通过购买我们的付费产品之一来支持我们。
我们非常欢迎您从家乡寄给我们一张明信片,并说明您正在使用我们哪些包。您可以在我们的联系页面上找到我们的地址。我们将把所有收到的明信片发布在我们的虚拟明信片墙上。
安装
您可以通过 composer 安装此包
composer require spatie/laravel-welcome-notification
迁移数据库
您必须通过执行此命令来发布此包提供的迁移
php artisan vendor:publish --provider="Spatie\WelcomeNotification\WelcomeNotificationServiceProvider" --tag="migrations"
接下来,您必须迁移您的数据库。
php artisan migrate
准备用户模型
您必须将 \Spatie\WelcomeNotification\ReceivesWelcomeNotification
特性应用到您的 User
模型。
准备 WelcomeController
接下来,您需要创建一个自己的控制器,该控制器将扩展 Spatie\WelcomeNotification\WelcomeController
。此控制器将用于显示欢迎表单和保存用户设置的密码。
namespace App\Http\Controllers\Auth; use Spatie\WelcomeNotification\WelcomeController as BaseWelcomeController; class MyWelcomeController extends BaseWelcomeController { }
注册路由
您必须注册这些路由
use Spatie\WelcomeNotification\WelcomesNewUsers; use App\Http\Controllers\Auth\MyWelcomeController; Route::group(['middleware' => ['web', WelcomesNewUsers::class,]], function () { Route::get('welcome/{user}', [MyWelcomeController::class, 'showWelcomeForm'])->name('welcome'); Route::post('welcome/{user}', [MyWelcomeController::class, 'savePassword']); });
准备欢迎表单视图
当有人点击欢迎通知邮件中的欢迎链接时,会渲染与包一起提供的 welcome
视图。您应该自行设计此视图。您可以使用此命令发布视图
php artisan vendor:publish --provider="Spatie\WelcomeNotification\WelcomeNotificationServiceProvider" --tag="views"
使用方法
以下是向您刚刚创建的用户发送欢迎通知的方法。
$expiresAt = now()->addDay(); $user->sendWelcomeNotification($expiresAt);
处理成功请求
用户成功设置新密码后,WelcomeController
的 sendPasswordSavedResponse
将被调用。
use Symfony\Component\HttpFoundation\Response; class MyWelcomeController extends BaseWelcomeController { public function sendPasswordSavedResponse(): Response { return redirect()->route('home'); } }
自定义通知
默认情况下,WelcomeNotification
将发送一封邮件。如果您希望自定义邮件,可以扩展 WelcomeNotification
并覆盖 buildWelcomeNotificationMessage
方法。
use Illuminate\Notifications\Messages\MailMessage; class MyCustomWelcomeNotification extends WelcomeNotification { public function buildWelcomeNotificationMessage(): MailMessage { return (new MailMessage) ->subject('Welcome to my app') ->action(Lang::get('Set initial password'), $this->showWelcomeFormUrl); } }
要使用自定义通知,您必须在您的 User
模型中添加一个名为 sendWelcomeNotification
的方法。
public function sendWelcomeNotification(\Carbon\Carbon $validUntil) { $this->notify(new MyCustomWelcomeNotification($validUntil)); }
验证额外字段
与此包一起提供的默认欢迎表单仅要求输入密码。您可以通过发布视图并在其中添加更多字段来添加更多字段。
要验证新字段,您可以在自己的 WelcomeController
中覆盖 rules
函数。以下是一个示例,其中我们想要验证一个名为 job_title
的额外字段。
class MyWelcomeController extends BaseWelcomeController { public function rules() { return [ 'password' => 'required|confirmed|min:6', 'job_title' => 'required', ]; } }
测试
composer test
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
请参阅CONTRIBUTING以获取详细信息。
安全
如果您发现有关安全性的错误,请通过security@spatie.be发送邮件,而不是使用问题跟踪器。
鸣谢
许可协议
MIT 许可协议(MIT)。请参阅许可文件以获取更多信息。