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,520 points
1,331 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,840 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,840 points
6,627 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,840 points
1,451 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,840 points
843 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,840 points
12,097 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,840 points
1,759 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,840 points
1,362 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,840 points
1,233 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,840 points
3,295 views
0 votes
1 answer

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

ghfghfgh skldjskldjakldjsklajd skdjakldjskljdsklajd' DFK ...READ MORE

answered Mar 1, 2022 in Laravel by ghghg

edited Mar 5 5,450 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,840 points
1,645 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