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
• 4,560 points
14 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
• 8,620 points

Related Questions In Laravel

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
• 8,620 points
19 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
• 8,620 points
42 views
0 votes
1 answer

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

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

answered Mar 23 in Laravel by Niroj
• 8,620 points
30 views
0 votes
1 answer

How to make a constant and use globally in laravel?

Hii, You can create a constants.php page in config folder ...READ MORE

answered Mar 24 in Laravel by Niroj
• 8,620 points
19 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
• 8,620 points
19 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
• 8,620 points
21 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
• 8,620 points
14 views
0 votes
1 answer

What are the important directories used in a common Laravel application

Hey @Kartik. Directories used in a common Laravel ...READ MORE

answered Mar 17 in Laravel by Niroj
• 8,620 points
20 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
• 8,620 points
30 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 in Laravel by Niroj
• 8,620 points
22 views