Explain Contextual Binding and how does it work

0 votes
Sometimes we may have two classes that utilize the same interface, but we wish to inject different implementations into each class.

How can this be possible?? Can anyone explain me out??

Thank You!
Mar 26, 2020 in Laravel by kartik
• 37,510 points
978 views

1 answer to this question.

0 votes

Hello,

Sometimes you may have two classes that utilize the same interface, but you wish to inject different implementations into each class. 

For example, two controllers may depend on different implementations of the Illuminate\Contracts\Filesystem\Filesystem contract. Laravel provides a simple, fluent interface for defining this behavior:

use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\PhotoController;
use App\Http\Controllers\VideoController;
use Illuminate\Contracts\Filesystem\Filesystem;

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

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

The above code will allow you to have two class that utilize the same interface.

Thank You!

answered Mar 26, 2020 by Niroj
• 82,880 points

Related Questions In Laravel

0 votes
1 answer

How to make Django serve that file for download as opposed to trying to find a URL and View to display it?

Hello @kartik, You can just use the built ...READ MORE

answered Jul 30, 2020 in Laravel by Niroj
• 82,880 points
6,142 views
0 votes
1 answer

Explain the concept of encryption and decryption in Laravel?

It is a process of transforming any ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,880 points
1,010 views
0 votes
1 answer

How can we use controller once it was created successfully?

Hey kartik You can  first refer how to  ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
527 views
0 votes
1 answer

How to pass data through URL and access through controller in Laravel?

Hello, You can  first refer how to  Create controller through ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
11,398 views
0 votes
1 answer

How to download and install Lavavel framework?

Hey @kartik, First you must have xampp install ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,880 points
1,084 views
0 votes
1 answer

Display Laravel in browser by using cmd promt?

Hello, First you need to have laravel install ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,880 points
939 views
0 votes
1 answer

How can we get started with Laravel through Xampp?

Hii, First you need to start Apache and ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,880 points
744 views
0 votes
1 answer

How to change Laravel official name to any customize name?

Hey, You just need to go Laravel folder through ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,880 points
2,605 views
0 votes
1 answer

Explain make Method in Laravel and what does the make() method do in Laravel?

Hey, You may use the make method to ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,880 points
4,636 views
0 votes
1 answer

Explain validations in laravel and How to validate my application incoming data?

Hey @kartik, In Programming validations are a handy ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
1,048 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP