'Laravel What is the use of service providers for 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



Solution 1:[1]

Service providers for laravel

Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel's core services are bootstrapped via service providers.

But, what do we mean by "bootstrapped"? In general, we mean registering things, including registering service container bindings, event listeners, middleware, and even routes. 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. Of course, many of these are "deferred" providers, meaning they will not be loaded on every request, but only when the services they provide are actually needed.

Imagine you have created a class which requires multiple dependencies and in general, you use it like this:

$foo = new Foo(new Bar(config('some_secret_key')), new Baz(new Moo(), new 
Boo()), new Woo('yolo', 5));

it's doable, but you wouldn't want to figure out these dependencies every time you try to instantiate this class. That's why you want to use a service provider wherein the register method you can define this class as:

$this->app->singleton('My\Awesome\Foo', function ($app) {
   return new Foo(new Bar(config('some_secret_key')), new Baz(new Moo(), new 
   Boo()), new Woo('yolo', 5));
});

This way, if you need to use this class, you can just type hint it in the controller (container will figure it out) or ask for it manually like

$foo = app(My\Awesome\Foo::class). Isn't that easier to use? ;)

below Link will guide you how to write your own service providers and register them and use with your Laravel application.

https://laravel.com/docs/5.7/providers

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Problem Solver