Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel 5: what is the purpose of service and providers directory

I'm currently working laravel5 for CRM application with the help of Repository method under Provider Directory. But i'm totally look blind to understand Service Directory, and purpose of this directory.

And can anyone give me example to utilize these directories by differentiate difference.

like image 974
Safoor Safdar Avatar asked Apr 02 '15 12:04

Safoor Safdar


2 Answers

Services

Services are re-usable classes that do not belong in a controller. For example, a service which is required by more than one controller such as class for building site navigation. It is a good place to put "global" classes (global to your app), which can be injected into a controller for use across your application.

Providers

Providers inject services into the dependency injection system, making them easier to access across the application. Packages which are Laravel specific usually include a service provider, which ensures that the packages classes are loaded when required and available to your controllers.

like image 172
Joe Avatar answered Sep 25 '22 00:09

Joe


Services (http://laravel.com/docs/5.0/structure)
The Services directory contains various "helper" services your application needs to function. For example, the Registrar service included with Laravel is responsible for validating and creating new users of your application. Other examples might be services to interact with external APIs, metrics systems, or even services that aggregate data from your own application.

Providers
The Providers directory purpose is basically binding the custom files with app, for example if we want to work with the repository pattern and use eloquent instead of write queries in models then we need to bind our repositories with service providers and register service provider in to config/app.php file.

like image 24
Usman Ali Avatar answered Sep 21 '22 00:09

Usman Ali