php - Laravel - 从中间件调用服务类 - 类 App\Http\Middleware\** 不存在
问题描述
所以我在这里有一些中间件:
namespace App\Http\Middleware;
use Closure;
use App\ChatLog;
use App\Http\Services;
class LogChat
{
protected $chatLogService;
public function __construct(ChatLogService $chatLogService)
{
$this->chatLogService = $chatLogService;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$chatLog = new ChatLog;
$chatLog->companyid = '5';
$chatLog->type = 'REQUEST';
$chatLog->ipaddress = '1';
$chatLog->name = 'bob';
$chatLog->message = 'testmessage';
$chatLog->action = 'click';
$chatLog->timeTaken = '1';
$chatLog->fullLog = 'all the log';
$this->chatLogService->store($chatLog);
return $next($request);
}
}
在这里调用此服务:
namespace App\Http\Services;
use App\ChatLog;
use Illuminate\Support\Facades\DB;
class ChatLogService
{
/**
* Display a listing of the resource.
*
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \App\ChatLog $chatLog
*/
public function store(ChatLog $chatLog)
{
$chatLog->save();
}
/**
* Display the specified resource.
*
* @param \App\ChatLog $chatLog
*/
public function show(ChatLog $chatLog)
{
//
}
}
在这里注入:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(ChatLogService::class, function ($app) {
return new ChatLogService();
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
}
但这给了我以下异常:ReflectionException Class App\Http\Middleware\ChatLogService 不存在
我想要做的是以一种非常抽象的方式调用“存储”方法,我觉得这个解决方案非常接近 - 但是,我似乎无法解决这个错误。我不确定我是否缺少一些简单的东西,或者该方法是否从根本上是错误的!
我看不出有什么问题!
塔:)
解决方案
推荐阅读
- typeorm - Typeorm - 类型化关系而不是字符串
- pandas - 打印两列,其中一列有条件?
- google-sheets - 获取最近日期的值
- c# - ASP.NET Core MVC POST 方法一个具有外键 ID 的模型
- javascript - Canvas toBlob 只是生成一个透明图像
- java - VS Code 在行尾插入分号并新建一行
- spring-boot - 如何在spring boot jdbc中从多个具有批量大小的表中执行批量删除
- android - Kotlin - 动态更改bottomNavigationView图标颜色
- c++ - 当函数调用返回函数指针类型时,[expr.call]/1 似乎错过了一种可能的结果。我对此是否正确?
- python - 正则表达式模式以匹配命令