Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel Notification·¢ËÍ֪ͨ£¿
laravelÊÇÒ»ÖÖÆÕ±éʹÓõÄphp webÓ¦ÓóÌÐò¿ò¼Ü£¬ËüÌṩÁËÏÖ´úµÄ¡¢ÓÅÑŵġ¢¹¦Ð§¸»ºñµÄÓï·¨À´¹¹½¨webÓ¦ÓóÌÐò¡£laravel¿ò¼ÜÌṩÆÕ±éµÄ¹¤¾ßºÍ¹¦Ð§£¬ÆäÖаüÀ¨Í¨ÖªµÄ·¢ËÍ¡£Í¨ÖªÊÇÏòÓû§»òÆäËûϵͳ·¢ËÍÊÓ¾õ»òÓïÒôÐÂÎŵÄÊÖÒÕ¡£Í¨Öª¿ÉÒÔÊǼòÆÓµÄÖÒÑÔ£¬Ò²¿ÉÒÔÊÇÖØ´óµÄָʾ£¬ËüÃÇ¿ÉÒÔÊÇÒì²½µÄ£¬Òò´Ë¿ÉÒÔÔÚ³¤Ê±¼äÖ´ÐеIJÙ×÷ÖÐʹÓá£
ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ñ§Ï°ÔõÑùʹÓÃLaravel NotificationÀ´·¢ËÍ֪ͨ¡£ ÎÒÃÇ»¹½«Ïàʶ֪ͨµÄÀàÐÍ¡¢ÊÂÎñºÍƵµÀ¡£ÎÒÃǵÄÄ¿µÄÊǽ¨ÉèÒ»¸ö¼òÆÓµÄWebÓ¦ÓóÌÐò£¬Ê¹ÓÃLaravel֪ͨ·¢ËÍ֪ͨ²¢ÏàʶËüÃǵIJî±ðÀàÐÍ¡£
Ìõ¼þÌõ¼þ
ÔÚ½øÈë±¾ÎÄÄÚÈÝ֮ǰ£¬ÎÒÃÇÐèҪȷ±£ÒÔÏÂÒªÇó»ñµÃÖª×㣺
ÄãÒѾÔÚÍâµØ»òЧÀÍÆ÷ÉÏ×°ÖÃÁËLaravel¡£
ÄãÒѾÊìϤLaravel¿ò¼Ü²¢ÏàʶÔõÑù½¨Éè¿ØÖÆÆ÷¡¢Â·ÓɺÍÊÓͼ¡£
ÄãÒѾװÖÃÁËÊý¾Ý¿â£¨MySQL¡¢PostgreSQLµÈ£©²¢ÔÚLaravelÉèÖÃÎļþÖоÙÐÐÁËÉèÖá£
ÄãÒѾÉèÖúÃÁËÓʼþÏà¹ØµÄÉèÖã¨Õâ¹ØÓÚһЩ֪ͨÀàÐÍÊDZØÐèµÄ£©¡£
Laravel Notifications
Laravel Notifications ÊÇÒ»¸öÇ¿Ê¢µÄÀàÀ´¼ò»¯´¦Àí֪ͨ¡£Í¨Öª¿ÉÒÔ·¢Ë͵½¶à¸öƵµÀ£¬°üÀ¨µç×ÓÓʼþ¡¢¶ÌÐÅ¡¢ÓʼþºÍSlackÕâÑùµÄ¼´Ê±ÐÂÎÅÓ¦ÓóÌÐò¡£Í¨¹ý Laravel Notifications£¬Äã¿ÉÒÔ½¨Éè¾ßÓÐÖ¸¶¨ÆµµÀµÄ֪ͨ£¬²¢¿ìËÙ½«Æä·¢Ë͸øÓû§¡£ ֪ͨ¿ÉÒÔÒì²½´¦Àí£¬ÒÔ×èÖ¹×îÏÈ·¢ËÍ֪ͨʱµÄÑÓ³Ù¡£
Laravel Notifications »ù±¾ÉÏÓÐÒÔÏÂÁ½¸ö²¿·Ö£º
ÐÂÎÅ×Ô¼º£ºÓÃÓÚÔÚÓ¦ÓóÌÐòÖÐÉùÃ÷²¢½ç˵֪ͨµÄÐÂÎÅÌå¡£
ͨµÀ£ºÓÃÓÚ·¢Ëͺʹ¦Àí²î±ðÀàÐ͵Ä֪ͨÐÂÎÅ¡£
´ó´ó¶¼¿ª·¢Õßͨ³£½«Í¨ÖªÀàÐͽç˵ΪӦÓóÌÐòÄÚµÄÄ£×ÓÒªÁ죬ÒÔ±ãÔÚÐèÒª·¢ËÍ֪ͨʱŲÓøÃÒªÁì¡£
ÔÚ Laravel Öн¨Éè֪ͨ
Laravel µÄ֪ͨ»ù±¾ÉÏÊǼòÆÓµÄPHPÀ࣬Ëü¼ÌÐøÁË¿ò¼ÜµÄ IlluminateNotificationsNotification base Àࡣÿ¸ö֪ͨÓÖÓжàÖÖͨµÀ¿ÉÒÔ·¢ËÍ£¬°üÀ¨µç×ÓÓʼþ¡¢Slack ºÍ×Ô½ç˵ webhook µÈ¡£
ҪʹÓÃ֪ͨ£¬ÔÚÄãµÄÓ¦ÓóÌÐòÖнç˵֪ͨÀà¡£ ֪ͨÀàÌåÏÖ֪ͨµÄ¸»Îı¾ÐÂÎÅ£¨ÐÂÎÅÕýÎÄ£©£¬ÔÚ֪ͨÐÂÎÅ·¢Ë͵½ÆµµÀ֮ǰ½«Æ佨ÉèΪһ¸öÐÂÎÅʵÀý¡£½ÓÏÂÀ´£¬Í¨¹ý²î±ðµÄͨµÀ·¢ËÍ´Ë֪ͨÒÔ¹©´¦Àí¡£
ÀýÈ磬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸öÌáÐÑÓû§Æ䶩Ôļ´½«µ½ÆÚµÄÐÂÎÅ֪ͨ¡£Òª·¢ËÍ´Ë֪ͨ£¬ÎÒÃÇ¿ÉÒÔÔÚ Lavarel Ó¦ÓóÌÐòÖн¨ÉèÒ»¸öеÄ֪ͨÀ࣬ºÃ±È£º
php artisan make:notification SubscriptionEnding
µÇ¼ºó¸´ÖÆ
ͨ¹ý֪ͨ·¢ËÍÓʼþ
ÏÖÔÚÎÒÃÇÒѾ½¨ÉèÁËÒ»¸ö SubscriptionEnding ֪ͨ£¬½ÓÏÂÀ´ÎÒÃÇ¿ÉÒÔͨ¹ýÓʼþ·¢Ë͸Ã֪ͨ¡£
ÄúÐèÒªÉèÖà Laravel µÄ Mail ϵͳÒԱ㷢Ë͵ç×ÓÓʼþ¡£Äú¿ÉÒÔͨ¹ýʹÓà Artisan À´È·±£ÄúµÄ SMTP/IMAP ÉèÖÃ׼ȷ£¬ÒÔ±ãÔÚÓ¦ÓóÌÐòÖÐʹÓÃÓʼþЧÀÍ¡£
php artisan config:cache
µÇ¼ºó¸´ÖÆ
ÏÖÔÚÎÒÃÇÒѾÉèÖúÃÁËÓʼþÏà¹ØµÄÉèÖã¬ÎÒÃÇÐèÒªÔÚ֪ͨÀàÖÐʵÏÖtoMail()ÒªÁ죬ÎÒÃÇÔÚÕâ¸öÒªÁìÖбàдÓʼþÄ£°å£º
class SubscriptionEnding extends Notification { use Queueable; public function __construct($data) { $this->data = $data; } public function via($notifiable) { return ['mail']; } public function toMail($notifiable) { return (new MailMessage) ->line('Hi ' . $this->data['name'] . ', your subscription is about to end in ' . $this->data['days'] . ' days. Please log in to your account and renew your subscription to continue using our services.') ->action('Renew Subscription', url('/subscription')) ->line('Thank you for using our service.'); } public function toArray($notifiable) { return [ // ]; } }
µÇ¼ºó¸´ÖÆ
ÈôÊÇÎÒÃÇÐèÒª×Ô½ç˵ÓʼþÄ£°åµÄ»°£¬¿ÉÒÔʹÓà Laravel µÄÓʼþÊÓͼÀ´±àд×Ô¼ºµÄ HTML/CSS Ä£°å£º
public function toMail($notifiable) { return (new MailMessage)->markdown('emails.subscription-ending', [ 'name' => $this->data['name'], 'days' => $this->data['days'] ]); }
µÇ¼ºó¸´ÖÆ
×îºóÒ»²½Êǽ«ÎÒÃǵÄ֪ͨ·¢Ë͸øij¸öÓû§£¬ÀýÈ磺
$user = User::find(1)->notify(new SubscriptionEnding(['name' => 'John Doe', 'days' => 5]));
µÇ¼ºó¸´ÖÆ
ÎÒÃÇÒѾÀֳɵØÔÚ¶©ÔÄ¿ìÓâÆÚʱ·¢ËÍÁËÒ»·âµç×ÓÓʼþ֪ͨ¡£Í¨¹ýƾ֤ÕâЩ°ì·¨£¬¿ÉÒÔÇáËɽ¨Éè×Ô½ç˵µÄ֪ͨ¡¢Îª´Ë֪ͨ±àд×Ô½ç˵ƵµÀºÍÐÂÎÅ£¬²¢Í¨¹ýÕâЩ֪ͨ·¢ËÍÓʼþ¡¢¶ÌÐźÍÆäËû֪ͨ¡£
ÔÚ·ÓÉÄ£×Ó°ó¶¨ÉÏʹÓÃ֪ͨ
·ÓÉÄ£×Ó°ó¶¨ÊÇ Laravel Ä£×ÓÖкÜÊÇÇ¿Ê¢µÄ¹¦Ð§Ö®Ò»¡£Ëùν·Óɰ󶨣¬¼´Èà Laravel ×Ô¶¯ÕÒµ½Æ¥ÅäÄ£×ÓµÄÆäËü°ó¶¨£¬½«Ëü×Ô¶¯×¢Èëµ½¿ØÖÆÆ÷ÒªÁìÖС£
ÔÚLaravelÖн«Å²ÓÃÄ£×ÓʵÀýµ½Â·ÓÉÖеİ󶨳ÆΪ¡°Â·ÓÉÄ£×Ӱ󶨡±¡£ÔÚ´ó´ó¶¼ÇéÐÎÏ£¬ÎÒÃÇÔÚ·ÓÉÖÐʹÓÃÒ»¸öÌض¨µÄ±êʶ·ûÀ´²éÕÒ¡¢¼ìË÷ºÍ»ñÈ¡¹¤¾ß¡£¿ÉÊÇ£¬ÔÚ·ÓÉÄ£×Ó°ó¶¨ÖУ¬Laravel»á×Ô¶¯½«Ä£×ÓÔØÈ뵽·ÓɵIJÎÊýÖС£
Laravel Notifications Ò²¿ÉÒÔʹÓ÷ÓÉÄ£×Ӱ󶨡£ÕâÊÇÒ»¸ö¼òÆÓµÄÀý×Ó£¬ÑÝʾÁËÔÚ·ÓÉÄ£×Ó°ó¶¨ÉÏʹÓà Notification¡£
ÎÒÃÇ¿ÉÒÔʹÓà Artisan ¾²Ì¬ÏÂÁî¿ìËÙ½¨ÉèʾÀý¿ØÖÆÆ÷ºÍ·ÓÉ£º
php artisan make:controller UserController --resource --no-model
µÇ¼ºó¸´ÖÆ
ÔÚÄ£×ÓÖнç˵ÉÏÏÂÎÄ£º
class User extends Authenticatable { public function routeNotificationForMail() { return $this->email; } }
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÔÚ·ÓÉÎļþÖнç˵£¬ºÃ±ÈÎÒÃǽ«Â·ÓÉ°²ÅÅÓÚ routes/web.php ÎļþÖУº
Route::get('users/{user}', 'UserController@show');
µÇ¼ºó¸´ÖÆ
ÔÚ¿ØÖÆÆ÷ÒªÁìÖÐʵÏÖ Notification£º
class UserController extends Controller { public function show(User $user) { $details = [ 'title' => 'ÄãºÃ°¡£¡', 'body' => 'ллÄãʹÓÃÎÒÃǵÄϵͳ£¡' ]; $user->notify(new AppNotificationsSimpleNotification($details)); return view('users.show', ['user' => $user]); } }
µÇ¼ºó¸´ÖÆ
Õ⽫ÏòÓû§·¢ËÍÒ»Ìõ֪ͨ£¬Í¨ÖªÓ¦°üÀ¨Ò»¸öÎÊÌâºÍÕýÎÄ¡£
½áÂÛ
Laravel Notification ÊÇÏòÄãµÄ Web Ó¦ÓóÌÐòÖÐÌí¼Ó֪ͨ¹¦Ð§µÄÓÐÓ÷½·¨¡£Í¨¹ý Laravel µÄǿʢϵͳ£¬ÎÒÃÇÄܹ»ÇáËɵؽç˵ºÍ´¦Àí֪ͨ£¬²¢Ê¹ÓÃÖݪֲî±ðµÄͨµÀÀ´·¢ËÍ֪ͨ¡£ ÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃÇÒѾѧϰÁËÔõÑùʹÓà Laravel Notification À´·¢ËÍ֪ͨ£¬°üÀ¨µç×ÓÓʼþ¡¢¶ÌÐÅ¡¢ÓʼþºÍ Slack¡£ ÎÒÃÇ»¹Ì½ÌÖÁË Laravel Notification µÄÆäËû¹¦Ð§£¬°üÀ¨Í¨ÖªÀàÐÍ¡¢ÊÂÎñºÍƵµÀ¡£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel Notification·¢ËÍ֪ͨ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡