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 in Laravel by kartik
• 27,360 points
70 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 by Niroj
• 58,960 points

Related Questions In Laravel

0 votes
1 answer
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 in Laravel by Niroj
• 58,960 points
84 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 in Laravel by Niroj
• 58,960 points
43 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 in Laravel by Niroj
• 58,960 points
1,770 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 in Laravel by Niroj
• 58,960 points
172 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 in Laravel by Niroj
• 58,960 points
67 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 in Laravel by Niroj
• 58,960 points
74 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 in Laravel by Niroj
• 58,960 points
81 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 in Laravel by Niroj
• 58,960 points
188 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 in Laravel by Niroj
• 58,960 points
108 views