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



这是完美的,但我删除了一些未使用的类,现在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 AppProviders;
use AppServicesCajaService;
use AppServicesPrecioService;
use AppServicesProductoService;
use AppServicesServicioService;
use AppServicesTrabajoService;
use AppServicesTurnoService;
use Calendar;
use IlluminateSupportServiceProvider;
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();
});
}
}

产品服务.php


namespace AppServices;
use AppProducto;
use AppServicesPrecioService;
class ProductoService
{
protected $precio_service;
public function __construct(PrecioService $precio_service)
{
$this->precio_service = $precio_service;
}
.
.
.
some more core
.
.
.

请将您的AppServiceProvider.php更改为低于

namespace AppProviders;
use AppServicesCajaService;
use AppServicesPrecioService;
use AppServicesProductoService;
use AppServicesServicioService;
use AppServicesTrabajoService;
use AppServicesTurnoService;
use Calendar;
use IlluminateSupportServiceProvider;
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({返回new PrecioService((;});希望这能解决您的疑问。如果你不清楚,你可以在下面发表评论。如果需要,我会详细解释。

最新更新