J
J
JohnDaniels2019-04-15 00:19:26
Laravel
JohnDaniels, 2019-04-15 00:19:26

How to properly use ServiceProvider in laravel?

I don't quite understand why they are needed.
For example I do this

use App\Http\Controllers\Controller;
use App\Services\MyService ;

class MyController extends Controller
{
    protected $myService;

    public function __construct(MyService $myService)
    {
        $this->myService= $myService;
    }
}

and everything works.
Why then create your own service providers and specify the binding in register() ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Popov, 2019-04-15
@be_a_dancer

In general, it is advisable to read about patterns when you apply them. I will try to briefly explain, although here, of course, you need to take the relevant literature and documentation and study the topic.
TL;DR
Service Provider is the class that is loaded first in your entire application and allows you to customize everything. Literally everything. Routes, middleware, and so on. For example, those two routing files that you use in your application are loaded in the RouteServiceProvider class.
A little more. Service providers are Laravel's implementation of the Dependency Injection pattern.. They allow you to set dependencies at the application build stage for their further use. In addition, service providers directly assemble the application. Any library for Laravel that you find on the Internet starts with a service provider , which determines the loading policy of the entire module.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question