1

我想了解 laravel 绑定。假设,我有 UploadFileController.php

Route::post('/upload/images', 'UploadFilesController@uploadImage');
Route::post('/upload/pdf', 'UploadFilesController@uploadPdf');

然后在控制器中,

class UploadFilesController extends Controller
{
   private $uploadServiceInterface;

   public function __construct(UploadServiceInterface $uploadServiceInterface)
   {
      $this->uploadServiceInterface = $uploadServiceInterface;
   }

   public function uploadImage(Request $request)
   {
          $this->uploadServiceInterface->store();
   }

   public function uploadPdf()
   {
          $this->uploadServiceInterface->store();
   }
}

现在,uploadServiceProvider,

class UploadServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->when(UploadFilesController::class)
            ->needs(UploadServiceInterface::class)
            ->give(ImagesUploadService::class);
    }

}

现在,我知道“何时”说带有 uploadService 接口的 UploadFileController 类将提供 imageUploadService 但我是否可以让它更具体地用于 uploadFileController 类中的功能,例如

$this->app->when(uploadFilesController::uploadImage())
->needs(UploadServiceInterface::class)
            ->give(ImagesUploadService::class);

然后它需要与 pdf 上传类相同的 imagesUploadService 类。

4

0 回答 0