Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel What is the use of service providers for laravel

Tags:

php

laravel

l am just getting start of Laravel, and l really confused about service contains and service providers l searched for some examples like follow's service code:

namespace App\Service;


class Tests
{
    public function test()
    {
        echo "aaa";
    }
}

serveice provider's code

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class TestServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
        $this->app->bind('App\Service\Tests', function($app){
            return new \App\Service\Tests();
        });
    }
}

Then l added this provider into config/app,php -> providers Then l create a controller

namespace App\Http\Controllers\test;

use App\Http\Controllers\Controller;
use App\Service\Tests as tests;

class Test extends Controller
{
    public function index()
    {
        $t = new tests();
        $t -> test();
    }
}

So, l can use my Tests like this, why l need to use it by Dependency injection like official site like:

public function index(tests $test)
{
    $test->test();
}

l saw some document or article about DI and IoC, but, but l just couldn't understand what is the use and the benefit about it

like image 422
afraid.jpg Avatar asked Oct 26 '18 02:10

afraid.jpg


People also ask

What is the use of service providers in Laravel?

Service providers are the central place to configure your application. If you open the config/app. php file included with Laravel, you will see a providers array. These are all of the service provider classes that will be loaded for your application.

What is the use of service provider?

A service provider is an individual or entity that provides services to another party. The provision of services between a service provider and a company is typically governed by a service agreement.

What is route service provider in Laravel?

All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by your application's App\Providers\RouteServiceProvider . The routes/web.php file defines routes that are for your web interface.

What is the purpose of register and boot methods in the service provider class?

Providers have two lifecycle methods: register and boot . The register method is where you should add bindings to the service container. The boot method is for performing actions after all service providers have registered their services.


1 Answers

First of all, Laravel uses service container and service providers, not server container or server provider :)

Here are some benefits of using dependencies injection (DI):

Simplify the object creation

Because your Test class constructor is quite simple, you don't see the benefit of dependencies injection. Think about a class like this:

class Complex {
    public function __construct(
        FooService $fooService,
        BarService $barService,
        int $configValue
    ) {

    }
}

Without DI, you have to get (or create) instances of $fooService and $barService, retrieve the value of $configValue from the configuration files every time you want a new instance of the Complex class.

With DI, you tell the service container how to create the Complex instance once, then the container can give the correct instance for you with one call (e.g. $container->make(Complex::class))

Manage the couplings between your classes

Continue with the previous example. What happens if the FooService and BarService depends on other classes, too?

Without DI, you have to create instances of the dependent objects (and hope that they do not depends on other classes). This usually ends with multiple instances of one class created, a waste of codes and computer memory.

With DI, all dependent objects are created by the container (you have to register those classes with the container before). The container also manages to keep only one instance of each class if you want, which save the amount of code as well as the amount of memory used by your program.

Only use one instance of your classes when registering with singleton

To keep only one instance of the class in the whole life of the current request, you can register your class creation process with singleton method instead of bind

like image 50
Hieu Le Avatar answered Oct 10 '22 21:10

Hieu Le