3

I am trying to understand laravel bind. let's say, I have UploadFileController.php

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

then in the controller,

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();
   }
}

Now, the uploadServiceProvider,

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

}

Now, I know "when" says that UploadFileController class with uploadService interface will give the imageUploadService but is it possible I make it more specific to function in uploadFileController class, like

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

then it takes to the imagesUploadService class same for pdf upload class.

Mohsin Younas
  • 324
  • 4
  • 12
  • You can try `$this->app->when(uploadFilesController::class.'@uploadImage')->needs...` but I have not tried it though so may not work. – apokryfos Nov 16 '20 at 01:23
  • @Mohsin Younas did you ever get a resolution to this?.. thanks. – jon Jan 08 '23 at 12:44

0 Answers0