删除未使用的类后,对Laravel 5.5的依赖注入无法正常工作 错误 AppServiceProvider.php ProductoService.php

这可以正常工作,但是我删除了一些未使用的类,现在laravel的自动装配功能无法解决类型提示的依赖关系。

我在AppServiceProvider.php中声明了服务类的各种绑定,这些依赖关系应通过自动装配来解决,但没有。

我留下一个示例代码AppServiceProvider.php和一个类ProductoService.php

我在这里想念的是什么?预先感谢!

错误

  

类型错误:函数参数太少   App \ Services \ ProductoService :: __ construct(),传入0   /home/eznb/Documentos/osiris/app/Providers/AppServiceProvider.php   第44行,恰好是1个

AppServiceProvider.php


namespace App\Providers;

use App\Services\CajaService;
use App\Services\PrecioService;
use App\Services\ProductoService;
use App\Services\ServicioService;
use App\Services\TrabajoService;
use App\Services\TurnoService;
use Calendar;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        view()->composer('*',function ($view) {

            $event_list = [];

            $calendar_details = Calendar::addEvents($event_list);
            $view->with('calendar_details',$calendar_details);
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(PrecioService::class,function ($app) {
            return new PrecioService();
        });
        $this->app->bind(ProductoService::class,function ($app) {
            return new ProductoService();
        });
        $this->app->bind(ServicioService::class,function ($app) {
            return new ServicioService();
        });
        $this->app->bind(CajaService::class,function ($app) {
            return new CajaService();
        });
        $this->app->bind(TrabajoService::class,function ($app) {
            return new TrabajoService();
        });
        $this->app->bind(TurnoService::class,function ($app) {
            return new TurnoService();
        });
    }
}

ProductoService.php


namespace App\Services;

use App\Producto;
use App\Services\PrecioService;

class ProductoService
{
    protected $precio_service;

    public function __construct(PrecioService $precio_service)
    {
        $this->precio_service = $precio_service;
    }
.
.
.
some more core
.
.
.

duxinaa 回答:删除未使用的类后,对Laravel 5.5的依赖注入无法正常工作 错误 AppServiceProvider.php ProductoService.php

请将您的AppServiceProvider.php更改为以下

 namespace App\Providers;
use App\Services\CajaService;
use App\Services\PrecioService;
use App\Services\ProductoService;
use App\Services\ServicioService;
use App\Services\TrabajoService;
use App\Services\TurnoService;
use Calendar;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        view()->composer('*',function ($view) {

            $event_list = [];

            $calendar_details = Calendar::addEvents($event_list);
            $view->with('calendar_details',$calendar_details);
        });
    }


    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(PrecioService::class,function ($app) {
            return new PrecioService();
        });
        $this->app->bind(ProductoService::class,function ($app) {
            return new ProductoService($app[PrecioService::class]); // Here i made the change as you ProductoService constructort has as its dependency
        });
        $this->app->bind(ServicioService::class,function ($app) {
            return new ServicioService();
        });
        $this->app->bind(CajaService::class,function ($app) {
            return new CajaService();
        });
        $this->app->bind(TrabajoService::class,function ($app) {
            return new TrabajoService();
        });
        $this->app->bind(TurnoService::class,function ($app) {
            return new TurnoService();
        });
    }

我已经使用$ app [PrecioService :: class]从服务容器中进行解析,因为您已在此行中注册$ this-> app-> bind(PrecioService :: class,function($ app){                 返回新的PrecioService();             }); 可能会解决您的查询。如果您不清楚,可以在下面评论。如果需要,我将详细解释。

本文链接:https://www.f2er.com/3159625.html

大家都在问