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
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.
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.
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.
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.
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):
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)
)
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With