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