<?php
//класс сервиса-----------------------------
namespace App\customService;
class customService {
public function test(){
echo '<br>кастомный сервис</br>';
}
}
//класс провайдера--------------------------
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use app\customService\customService;
class customServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('bar', function ($app) {
return new customService;
});
}
public function boot()
{
//
}
}
//контроллер в котором я хочу получить сервис
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
//зависимость
//use App\customService\customService;
class mainController extends Controller
{
//если этот конструктор с инъекций закоментирован ,
// и в методе main НЕ закоментирован $obj = \App::make('bar');
//то выпадает ошибка - Class 'app\customService\customService' not found
//если конструктор закомментирован - и в методе main закоментировать $obj = \App::make('bar');
//то все работает
//я не пойму как связаны инъекция в конструкторе и вызов объекта из контейнера по ключу 'bar'?
// public function __construct(customService $myService){
// $this->myService = $myService;
// }
public function main(){
echo '555';
$obj = \App::make('bar');
$obj->test();
}
}