Laravel - Вызов класса обслуживания из Middleware - Class App \ Http \ Middleware \ ** не существует - PullRequest
0 голосов
/ 04 мая 2020

Итак, у меня есть некоторое промежуточное ПО здесь:


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 не существует

То, что я хочу сделать, это вызвать метод "store" красиво абстрагированным образом, и я чувствую, что это решение очень близко - однако, я не могу обойти эту ошибку. Я не уверен, что это что-то простое, что я пропускаю, или подход в корне неверен!

Я не вижу, что не так!

Та :)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...