Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.7] Add support for multiple classes on contextual binding #25891

Conversation

fetzi
Copy link
Contributor

@fetzi fetzi commented Oct 3, 2018

I often need to add contextual binding for different non standard use cases. When doing this I recognized that I often have to define multiple contextual bindings for the same interface.

Consider the following example:

$this->app->when(PhotoController::class)
          ->needs(Filesystem::class)
          ->give(function () {
              return Storage::disk('s3');
          });

$this->app->when(VideoController::class)
          ->needs(Filesystem::class)
          ->give(function () {
              return Storage::disk('s3');
          });

$this->app->when(UploadController::class)
          ->needs(Filesystem::class)
          ->give(function () {
              return Storage::disk('local');
          });

This pull request adds the possibility to define the contextual binding for PhotoController and VideoController at once and should ease and shorten the service container bindings.

$this->app->when([PhotoController::class, VideoController::class])
          ->needs(Filesystem::class)
          ->give(function () {
              return Storage::disk('s3');
          });

$this->app->when(UploadController::class)
          ->needs(Filesystem::class)
          ->give(function () {
              return Storage::disk('local');
          });

allow contextual binding for multiple classes at once
@fetzi fetzi changed the title Add support for multiple classes on contextual binding [5.7] Add support for multiple classes on contextual binding Oct 3, 2018
@taylorotwell taylorotwell merged commit 8004070 into laravel:5.7 Oct 3, 2018
@fetzi fetzi deleted the feature/add-contextual-binding-for-multiple-classes branch October 3, 2018 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants