如何在Voyager中创建不关联模型的视图并添加后台菜单实现Firebase推送
实现步骤
1. 创建自定义控制器
在app/Http/Controllers/Admin目录下新建FirebaseNotificationController.php,代码如下:
<?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class FirebaseNotificationController extends Controller { // 显示通知发送表单 public function showForm() { return view('admin.send-notification'); } // 处理表单提交逻辑 public function send(Request $req) { // 表单校验 $req->validate([ 'title' => 'required', 'message' => 'required' ]); $url = 'https://fcm.googleapis.com/fcm/send'; $dataArr = array( 'click_action' => 'FLUTTER_NOTIFICATION_CLICK', 'id' => $req->id, 'status'=>"done" ); $notification = array( 'title' =>$req->title, 'text' => $req->message, 'sound' => 'default', 'badge' => '1', ); $arrayToSend = array( 'to' => "/topics/all", 'notification' => $notification, 'data' => $dataArr, 'priority'=>'high' ); $fields = json_encode ($arrayToSend); $headers = array ( 'Authorization: key=' . env('FCM_SERVER_KEY'), 'Content-Type: application/json' ); $ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, $url ); curl_setopt ( $ch, CURLOPT_POST, true ); curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields ); $result = curl_exec ($ch); curl_close ($ch); return back()->with([ 'message' => '通知发送成功:'.$result, 'alert-type' => 'success' ]); } }
注意:不要硬写Firebase密钥,在项目.env文件中新增一行FCM_SERVER_KEY=你的Firebase服务密钥即可
2. 注册路由
在routes/web.php文件中添加如下路由,使用Voyager自带的admin.user中间件做权限校验:
Route::group(['prefix' => 'admin', 'middleware' => 'admin.user'], function () { Route::get('/send-notification', [\App\Http\Controllers\Admin\FirebaseNotificationController::class, 'showForm'])->name('admin.send-notification.form'); Route::post('/send-notification', [\App\Http\Controllers\Admin\FirebaseNotificationController::class, 'send'])->name('admin.send-notification.send'); });
3. 创建适配Voyager样式的表单视图
在resources/views/admin目录下新建send-notification.blade.php,继承Voyager的后台母版:
@extends('voyager::master') @section('page_title', '发送Firebase通知') @section('content') <div class="page-content container-fluid"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <div class="panel panel-bordered"> <div class="panel-heading"> <h3 class="panel-title">发送全站通知</h3> </div> <form action="{{ route('admin.send-notification.send') }}" method="POST"> @csrf <div class="panel-body"> {{-- 操作提示 --}} @if(session()->has('message')) <div class="alert alert-{{ session('alert-type') }}"> {{ session('message') }} </div> @endif {{-- 标题输入框 --}} <div class="form-group"> <label for="title">通知标题</label> <input type="text" name="title" id="title" class="form-control" required value="{{ old('title') }}"> @error('title') <span class="text-danger">{{ $message }}</span> @enderror </div> {{-- 内容输入框 --}} <div class="form-group"> <label for="message">通知内容</label> <textarea name="message" id="message" rows="5" class="form-control" required>{{ old('message') }}</textarea> @error('message') <span class="text-danger">{{ $message }}</span> @enderror </div> </div> <div class="panel-footer"> <button type="submit" class="btn btn-primary">发送通知</button> </div> </form> </div> </div> </div> </div> @endsection
4. 在Voyager后台添加自定义菜单
- 进入Voyager后台,依次点击「工具」-「菜单生成器」,找到
admin菜单点击右侧的「生成」按钮 - 点击「新建菜单项」,按如下参数填写:
- 标题:发送Firebase通知
- 路由类型:URL
- 路径:
/admin/send-notification - 图标:可选择Voyager自带的
voyager-bell图标 - 权限:如果需要单独控制权限可选择对应权限项,默认选
browse_admin即可
- 保存后拖动菜单项到你想要的排序位置即可
5. 单独权限配置(可选)
如果需要给这个功能分配独立的访问权限,可进入「工具」-「权限」页面新建权限,slug填写send_notifications,之后给对应角色分配该权限,再把路由的中间件改为['middleware' => ['admin.user', 'can:send_notifications']]即可。
内容的提问来源于stack exchange,提问作者محمد عزاوي
相关产品推荐
相关产品推荐

