【问题标题】:Why I can not call Implementation method from library class?为什么我不能从库类中调用实现方法?
【发布时间】:2022-10-04 17:31:59
【问题描述】:

在 laravel 9 应用程序中,我创建了类 app/Implementations/LocalStorageUploadedFileManagement.php :

<?php

namespace App\Implementations;

use App\Interfaces\UploadedFileManagement;
...
use Intervention\Image\Facades\Image as Image;

class LocalStorageUploadedFileManagement implements UploadedFileManagement
{
    ...
    public function getImageFileDetails(string $itemId, string $image = null, string $itemUploadsDirectory = '',
        bool $skipNonExistingFile = false): array {
        \Log::info('++ getImageFileDetails$image ::');
        \Log::info( $image );

    }
    ...

和 app/Interfaces/UploadedFileManagement.php 中的接口类:

<?php

namespace App\Interfaces;

use Illuminate\Http\Request;

interface UploadedFileManagement
{
    ...
    public function getImageFileDetails(string $itemId, string $image = null, string $itemUploadsDirectory = '',
        bool $skipNonExistingFile = false): array;

}

在 app/Providers/AppServiceProvider.php 我有:

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind('App\Interfaces\UploadedFileManagement', 'App\Implementations\LocalStorageUploadedFileManagement');
        
    }

所以我可以在我的控制器 app/Http/Controllers/Admin/ProductController.php 中使用它:

<?php

namespace App\Http\Controllers\Admin;
...
use App\Interfaces\UploadedFileManagement;

class ProductController extends Controller  // http://local-mng-products.com/admin/products
{

    public function __construct()
    {
    }

    public function edit(string $productId, UploadedFileManagement $uploadedFileManagement)
    {
        ...
        $productImgProps = $uploadedFileManagement->getImageFileDetails(
            itemId : $product->_id,
            itemUploadsDirectory : Product::getUploadsProductDir(),
            image : $product->image,
            skipNonExistingFile : true);
        
        ...

abd 它工作正常

但我得到了错误:

Cannot instantiate interface App\Interfaces\UploadedFileManagement

当我尝试在库 app/Library/ReportProduct.php 类中使用 UploadedFileManagement 时:

<?php

namespace App\Library;

use App\Interfaces\UploadedFileManagement;
...
class ReportProduct
{

    public function getImageProps()
    {
        $uploadedFileManagement= new UploadedFileManagement();  // This line raised the error
        $imgProps = $uploadedFileManagement->getImageFileDetails($this->product->_id, $this->product->image, Product::getUploadsProductDir(),true);
        return $imgProps;
    }

getImageProps 方法是从我使用命令创建的 ProductCardReport 组件调用的:

php artisan make:component  ProductCardReport

它在文件 app/View/Components/ProductCardReport.php 中有:

<?php

namespace App\View\Components;

use App\Library\ReportProduct;
use Illuminate\View\Component;

class ProductCardReport extends Component
{
    public string $productId;

    public function __construct(string $productId)
    {
        $this->productId = $productId;
    }

    public function render()
    {
        $reportProduct = new ReportProduct($this->productId);
        $reportProduct->loadProduct();
        $productData = $reportProduct->getProductData(true);

        $productImgProps = $reportProduct->getImageProps();
        ...

    

为什么我在使用 UploadedFileManagement 服务时在 ReportProduct.php 类中出现错误?

那怎么办?

谢谢!

【问题讨论】:

    标签: laravel laravel-components laravel-service-container


    【解决方案1】:

    你的 UploadedFileManagement 是一个对象 interface 并且接口是不可实例化的(一种说你不能创建接口实例的奇特方式 - 即new UploadedFileManagement())。

    在 Laravel 服务容器中绑定 interfaceimplementation 并不会神奇地将您的代码转换为使用 implementation,无论您使用了 interface。它所做的只是告诉 Laravel 在遇到依赖项时如何解决它。

    在运行应用程序时澄清这一点

    $service = new UploadedFileManagement();
    

    不会转换为

    $service = new LocalStorageUploadedFileManagement();
    

    这不是依赖注入或服务容器的工作方式。

    如果你想在ReportProduct 类中使用你的LocalStorageUploadedFileManagement() 类,你需要以与ProductControlleredit 函数相同的方式注入它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多