php - 删除未使用的类后,Laravel 5.5 上的依赖注入不起作用
问题描述
这工作完美无缺,但我删除了一些未使用的类,现在 laravel 的自动装配功能无法解决类型提示的依赖关系。
我已经在AppServiceProvider.php
服务类的各种绑定中声明了哪些依赖项应该通过自动装配来解决,但没有。
我留下一个示例代码AppServiceProvider.php
和一个类ProductoService.php
我在这里缺少什么?提前致谢!
错误
类型错误:函数 App\Services\ProductoService::__construct() 的参数太少,在第 44 行的 /home/eznb/Documentos/osiris/app/Providers/AppServiceProvider.php 中传递了 0,而预期的正是 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();
});
}
}
产品服务.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
.
.
.
解决方案
请将您的 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) { return new PrecioService() ; }); 愿这能解决您的疑问。如果不清楚可以在下方评论。如果需要,我会详细解释。
推荐阅读
- ruby-on-rails - Rails 应用程序在负载过重时不断崩溃
- php - 删除特定单词之前的所有单词
- android - 如何在 viewpager 中添加意图
- bash - 将 bash 函数自动添加到 $PATH 或 bash shell
- javascript - 使用上一个/下一个按钮时更改活动选项卡颜色
- laravel - Laravel.log 文件不更新日志
- mysql - 如何将一个 Mysql 行显示为多行?
- json - 将特定 API 数据写入 JSON 文件
- javascript - 对象作为 React 子对象无效(发现:[object Promise])Google Maps
- javascript - 视频播放完毕后如何添加自定义按钮