laravel dependency injection code example

Example 1: how to use service container in laravel

use App\Services\Transistor;

$this->app->resolving(Transistor::class, function ($api, $app) {
    // Called when container resolves objects of type "HelpSpot\API"...
});

$this->app->resolving(function ($object, $app) {
    // Called when container resolves object of any type...
});

Example 2: laravel dependency injection

<?php

class Service
{
    //
}r

Route::get('/', function (Service $service) {
    die(get_class($service));
});

Example 3: laravel dependency injection

<?php

class Service
{
    //
}

Route::get('/', function (Service $service) {
    die(get_class($service));
});

Tags:

Php Example