删除未使用的类后,对Laravel 5.5的依赖注入不起作用

问题描述 投票:0回答:1

直到昨天,它的工作都完美无缺,但是我删除了一些未使用的类,现在laravel的自动装配功能无法解决类型提示的依赖关系。

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

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

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

错误

Type error: Too few arguments to function App\Services\ProductoService::__construct(), 0 passed in /home/eznb/Documentos/osiris/app/Providers/AppServiceProvider.php on line 44 and exactly 1 expected

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
.
.
.

php laravel inversion-of-control autowired
1个回答
0
投票

请将您的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();
        });
    }
© www.soinside.com 2019 - 2024. All rights reserved.