1.自定义notification的channel:
参考:
通过 Laravel 消息通知使用 EasySms 短信服务,让你的代码更简洁
详细:
Laravel 5.3 自定义 Notification Channel
Laravel 5.3 引入了全新的 Notification 通知系统,该系统默认支持 mail,database 和 array 三种
channel
,但是在实际的开发中,这些默认的channel
有可能满足实际的需求,比如在国内使用邮件发送的时候,该如何使用Sendcloud
来集成,使得Notification
也可以正常使用起来。这个时候可以使用下面的方法来自定义Channel
:1.在
app/
目录下创建Channels/
目录,并在该目录内创建自定义的Channel
类,比如SendCloudChannel
:1 namespace AppChannels; 2 3 4 use IlluminateNotificationsNotification; 5 6 7 /** 8 9 * Class SendCloudChannel 10 11 * @package AppChannels 12 13 */ 14 15 class SendCloudChannel 16 17 { 18 19 /** 20 21 * @param $notifiable 22 23 * @param Notification $notification 24 25 */ 26 27 public function send($notifiable, Notification $notification) 28 29 { 30 31 $message = $notification->toSendcloud($notifiable); // 注意这个 toSendcloud 的方法,在使用的时候要一致 32 33 } 34 35 }注意在这个类里面,一定要实现
send()
这个方法。然后在有了这个SendCloudChannel
类之后,在使用的使用可以直接以SendCloudChannel::class
的方式来使用,比如 在NewUserFollowNotification
中可以这样使用:1 class NewUserFollowNotification extends Notification 2 3 { 4 5 public function via($notifiable) 6 7 { 8 9 return [SendCloudChannel::class,'database']; // 注意这里面的 sendCloudChannel::class 就是我们自定义的 10 11 } 12 13 14 public function toSendcloud($notifiable) 15 16 { 17 18 // 发送邮件的具体代码. 19 20 } 21 22 }注意
toSendcloud($notifiable)
自定义SendCloudChannel
中send()
方法体的$notification->toSendcloud($notifiable)
的两个toSendcloud
名字要一致。
如果用默认的:
1 <?php 2 3 namespace AppNotifications; 4 5 use AppUser; 6 use IlluminateBusQueueable; 7 use IlluminateContractsQueueShouldQueue; 8 use IlluminateNotificationsMessagesMailMessage; 9 use IlluminateNotificationsNotification; 10 11 class NewUserFollowNotification extends Notification 12 { 13 use Queueable; 14 /** 15 * @var User 16 */ 17 private $user; 18 19 /** 20 * Create a new notification instance. 21 * 22 * @param $user 23 */ 24 public function __construct($user) 25 { 26 // 27 $this->user = $user; 28 } 29 30 /** 31 * Get the notification's delivery channels. 32 * 33 * @param mixed $notifiable 34 * @return array 35 */ 36 public function via($notifiable) 37 { 38 return ['mail', 'database']; 39 } 40 41 /** 42 * Get the mail representation of the notification. 43 * 44 * @param mixed $notifiable 45 * @return IlluminateNotificationsMessagesMailMessage 46 */ 47 public function toMail($notifiable) 48 { 49 return (new MailMessage) 50 ->line('The introduction to the notification.') 51 ->action('Notification Action', url('/')) 52 ->line('Thank you for using our application!'); 53 } 54 55 /** 56 * Get the array representation of the notification. 57 * 58 * @param mixed $notifiable 59 * @return array 60 */ 61 public function toArray($notifiable) 62 { 63 return [ 64 // 65 ]; 66 } 67 68 69 /** 70 * 添加一个方法,方法名要与via中添加的database 一致 to加上Database 71 * @param $notifiable 72 * @return array 73 */ 74 public function toDatabase($notifiable) 75 { 76 return [ 77 //要记录的数据 78 'name' => $this->user->name, 79 ]; 80 } 81 } 82 83
注意:修改下之前有个小bug
回答的用户信息页面和提问者信息页面的参数都是用的$question的,不正确:
1 @extends('layouts.app') 2 @section('content') 3 <div class="container"> 4 <div class="row"> 5 <div class="col-md-8 col-md offset-1"> 6 {{--问题--}} 7 <div class="card"> 8 <div class="card-header"> 9 {{ $question->title }} 10 11 @foreach(['success','warning','danger'] as $info) 12 @if(session()->has($info)) 13 <div class="alert alert-{{$info}}">{{ session()->get($info) }}</div> 14 @endif 15 @endforeach 16 17 @can('update',$question) 18 <a href="{{ route('questions.edit',$question) }}" class="btn btn-warning">编辑</a> 19 @endcan 20 21 @can('destroy',$question) 22 <form action="{{ route('questions.destroy',$question) }}" method="post"> 23 @csrf 24 @method('DELETE') 25 <button type="submit" class="btn btn-danger">删除</button> 26 </form> 27 @endcan 28 29 @forelse($question->topics as $topic) 30 <button class="btn btn-secondary float-md-right m-1">{{ $topic->name }}</button> 31 @empty 32 <p class="text text-warning float-md-right"> "No Topics"</p> 33 @endforelse 34 35 <p class="text text-info float-md-right"> 已有{{ count($question->answers) }}个回答</p> 36 37 </div> 38 <div class="card-body"> 39 {!! $question->content !!} 40 </div> 41 </div> 42 43 44 {{--回答提交form--}} 45 {{--只有登录用户可以提交回答--}} 46 @if(auth()->check()) 47 <div class="card mt-2"> 48 <div class="card-header"> 49 提交回答 50 </div> 51 <div class="card-body"> 52 <form action="{{ route('answers.store',$question) }}" method="post"> 53 @csrf 54 <!-- 回答编辑器容器 --> 55 <script id="container" name="content" type="text/plain" 56 style=" 100%;height: 200px">{!! old('content') !!}</script> 57 <p class="text text-danger"> @error('content') {{ $message }} @enderror </p> 58 <!--提交按钮--> 59 <button type="submit" class="btn btn-primary float-md-right mt-2">提交回答</button> 60 </form> 61 </div> 62 </div> 63 @else 64 {{--显示请登录--}} 65 <a href="{{ route('login') }}" class="btn btn-success btn-block mt-4">登录提交答案</a> 66 @endif 67 {{--展示答案--}} 68 @forelse($question->answers as $answer) 69 <div class="card mt-4"> 70 <div class="card-header"> 71 @include('users._small_icon',['userable'=>$answer]) 72 <span 73 class="float-right text text-info text-center">{{ $answer->updated_at->diffForHumans() }}</span> 74 </div> 75 76 <div class="card-body"> 77 {!! $answer->content !!} 78 </div> 79 </div> 80 81 @empty 82 83 @endforelse 84 </div> 85 86 <div class="col-md-3"> 87 <div class="card"> 88 <div class="card-header"> 89 <h2> {{ $question->followers_count }}</h2> 90 <span>关注者</span> 91 </div> 92 <div class="card-body"> 93 <question-follow-button question="{{$question->id}}"id}}"> 94 </question-follow-button> 95 </div> 96 </div> 97 98 <div class="card mt-4"> 99 <div class="card-header"> 100 <h2> 提问者 </h2> 101 </div> 102 <div class="card-body"> 103 @include('users._small_icon',['userable'=>$question]) 104 </div> 105 @include('users._user_stats') 106 </div> 107 </div> 108 109 110 </div> 111 </div> 112 @endsection 113 @section('footer-js') 114 @include('questions._footer_js') 115 @endsection 116 117
1 <div> 2 <img src="{{ $userable->user->avatar }}" class="card-img img-thumbnail imgWrap " 3 style=" 50px" alt="{{ $userable->user->name }}"> 4 <span class="text text-info">{{ $userable->user->name }}</span> 5 </div> 6 <div class="float-left mt-2"> 7 <user-follow-button user="{{ $userable->user->id }}"user->id }}"></user-follow-button> 8 @if(auth()->user()->id!==$userable->user->id) 9 <div class="float-right"> 10 <a href="#" class="btn btn-sm btn-info ml-2">私信</a> 11 </div> 12 @endif 13 </div> 14
将_small_icon.blade.php里面的$question换成$userable ,然后再在show.blade.php中将:
1 @include('users._small_icon') 2 3 换成 4 5 @include('users._small_icon',['userable'=>$answer]) 6 7 及 8 9 @include('users._small_icon',['userable'=>$question]) 10
不然提问的以及回答都是$question的user用户信息。传参数的原理说明:Laravel Blade 模板中的 include 方法基本使用