首页 > 解决方案 > 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 不存在

我想要做的是以一种非常抽象的方式调用“存储”方法,我觉得这个解决方案非常接近 - 但是,我似乎无法解决这个错误。我不确定我是否缺少一些简单的东西,或者该方法是否从根本上是错误的!

我看不出有什么问题!

塔:)

标签: phplaravelexceptionservicemiddleware

解决方案


推荐阅读