fork download
  1. <?php
  2.  
  3. //класс сервиса-----------------------------
  4.  
  5. namespace App\customService;
  6.  
  7.  
  8. class customService {
  9.  
  10. public function test(){
  11. echo '<br>кастомный сервис</br>';
  12. }
  13.  
  14. }
  15.  
  16. //класс провайдера--------------------------
  17.  
  18. namespace App\Providers;
  19.  
  20. use Illuminate\Support\ServiceProvider;
  21.  
  22. use app\customService\customService;
  23.  
  24. class customServiceProvider extends ServiceProvider
  25. {
  26.  
  27. public function register()
  28. {
  29. $this->app->bind('bar', function ($app) {
  30. return new customService;
  31. });
  32. }
  33.  
  34. public function boot()
  35. {
  36. //
  37. }
  38. }
  39.  
  40. //контроллер в котором я хочу получить сервис
  41.  
  42. namespace App\Http\Controllers;
  43. use App\Http\Controllers\Controller;
  44. use Illuminate\Http\Request;
  45.  
  46. //зависимость
  47. //use App\customService\customService;
  48.  
  49. class mainController extends Controller
  50. {
  51. //если этот конструктор с инъекций закоментирован ,
  52. // и в методе main НЕ закоментирован $obj = \App::make('bar');
  53. //то выпадает ошибка - Class 'app\customService\customService' not found
  54. //если конструктор закомментирован - и в методе main закоментировать $obj = \App::make('bar');
  55. //то все работает
  56.  
  57. //я не пойму как связаны инъекция в конструкторе и вызов объекта из контейнера по ключу 'bar'?
  58.  
  59. // public function __construct(customService $myService){
  60.  
  61. // $this->myService = $myService;
  62. // }
  63.  
  64. public function main(){
  65. echo '555';
  66. $obj = \App::make('bar');
  67.  
  68. $obj->test();
  69. }
  70. }
  71.  
  72.  
  73.  
  74.  
Runtime error #stdin #stdout #stderr 0.02s 24320KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
PHP Fatal error:  Uncaught Error: Class 'Illuminate\Support\ServiceProvider' not found in /home/sHhou0/prog.php:24
Stack trace:
#0 {main}
  thrown in /home/sHhou0/prog.php on line 24