Undefined property exception when trying to instantiate dependency from IOC container

cmvr

I'm trying to deepen my knowlade in laravel architecture.

  1. I have a search engine (elastic search for the sake of the example), but this search engine might change in the future. So im trying to write a container for this, so in case i'll change the engine in the future, i will have to change only the container. (I believe the termenology is factory design?)

  2. I have created a provider app/providers/DataFromSearchEngine.php that looks like this:

    use Illuminate\Support\ServiceProvider;
    
    class DataFromSearchEngine extends ServiceProvider {
       public function boot()
       {
        //
       }
    
       public function register()
      {
        $this->app->singleton('SearchEngine', function($app) {
           return new elasticSearch; 
        });
      }
    
    }
    
  3. Then i registered it in the providers array in config/app.php .

    'providers' => [
        // providers...
    
        'App\Providers\DataFromSearchEngine'
    ],
    
  4. The next step is to call SearchEngine from my controller:

    namespace App\Http\Controllers;
    
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    
    class SearchController extends Controller {
    
       protected $searchEngine;
    
       public function __construct() {
          $this->searchEngine = $this->app->make('SearchEngine');
       }
    
    }
    

But all these yields: Undefined property: App\Http\Controllers\SearchController::$app

Can someone explain what i'm missing?

Matt Burrow

Instead of using $this->app try using app().

This is because non of the inherited controller classes, i.e. App\Http\Controllers\Controller or Illuminate\Routing\Controllers\Controller have an app property on them.

As a note you can use app('SearchEngine') which is the equivalent of app()->make('SearchEngine') as a shortcut to making your object.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

"Dependency is not satisfiable" error when trying to install an application

분류에서Dev

React에서 'Type Error : Cannot Read Property'name 'of undefined when trying to access properties of an object'수정 방법

분류에서Dev

Getting an exception when trying to get the content of a file from response Web API2 WPF

분류에서Dev

Exception when trying to refresh Clojure code in cider

분류에서Dev

NullPointer Exception When Trying to Update Sprite

분류에서Dev

java.lang.RuntimeException: Unable to instantiate activity error when trying to switch between activities

분류에서Dev

Getting undefined method for nil:NilClass when trying to acess an instance varible

분류에서Dev

Exception when trying to run a map of LinkedLists through a scanner

분류에서Dev

Argument Null Exception error when trying to create a Text File

분류에서Dev

PHP function giving me exception when trying to display on HTML

분류에서Dev

PHP dependency property scope

분류에서Dev

Uncaught TypeError: Cannot read property 'create' of undefined while trying to loop through json array in jquery

분류에서Dev

Exception when make object from Skype in .NET?

분류에서Dev

Exception when removing items from an ArrayList

분류에서Dev

AngularJS - is there any dependency injection container?

분류에서Dev

Undefined value when calling an input from a HTML form from PHP

분류에서Dev

"Undefined reference to" Linking errors when trying to build with headers (QPID/Proton C++)

분류에서Dev

CodeIgniter Undefined property

분류에서Dev

When iam trying to run spring example i am facing the following exception

분류에서Dev

Why i'm getting "titlebarViewController not supported for this window style" exception when trying to add title bar accessory view

분류에서Dev

AngularJS 2-내부 속성 데이터 바인딩-EXCEPTION : TypeError : Cannot read property of undefined in

분류에서Dev

dependency problems, when updating

분류에서Dev

GoogleMap null when trying to access it from another fragment

분류에서Dev

Getting black screen when trying to remove ViewController from display in iOS

분류에서Dev

Errno 10048 when trying to read again from network through Bacpypes

분류에서Dev

Crash when trying to get Video Stream from IP Camera

분류에서Dev

“Unexpected error” when trying to get back Chrome OS from Ubuntu

분류에서Dev

ERROR TypeError : _co.userData is undefined In angular 7 when I try to use property binding for value

분류에서Dev

When dragging and dropping a file, the dataTransfer.items property is undefined in Firefox and IE, but not Chrome

Related 관련 기사

  1. 1

    "Dependency is not satisfiable" error when trying to install an application

  2. 2

    React에서 'Type Error : Cannot Read Property'name 'of undefined when trying to access properties of an object'수정 방법

  3. 3

    Getting an exception when trying to get the content of a file from response Web API2 WPF

  4. 4

    Exception when trying to refresh Clojure code in cider

  5. 5

    NullPointer Exception When Trying to Update Sprite

  6. 6

    java.lang.RuntimeException: Unable to instantiate activity error when trying to switch between activities

  7. 7

    Getting undefined method for nil:NilClass when trying to acess an instance varible

  8. 8

    Exception when trying to run a map of LinkedLists through a scanner

  9. 9

    Argument Null Exception error when trying to create a Text File

  10. 10

    PHP function giving me exception when trying to display on HTML

  11. 11

    PHP dependency property scope

  12. 12

    Uncaught TypeError: Cannot read property 'create' of undefined while trying to loop through json array in jquery

  13. 13

    Exception when make object from Skype in .NET?

  14. 14

    Exception when removing items from an ArrayList

  15. 15

    AngularJS - is there any dependency injection container?

  16. 16

    Undefined value when calling an input from a HTML form from PHP

  17. 17

    "Undefined reference to" Linking errors when trying to build with headers (QPID/Proton C++)

  18. 18

    CodeIgniter Undefined property

  19. 19

    When iam trying to run spring example i am facing the following exception

  20. 20

    Why i'm getting "titlebarViewController not supported for this window style" exception when trying to add title bar accessory view

  21. 21

    AngularJS 2-내부 속성 데이터 바인딩-EXCEPTION : TypeError : Cannot read property of undefined in

  22. 22

    dependency problems, when updating

  23. 23

    GoogleMap null when trying to access it from another fragment

  24. 24

    Getting black screen when trying to remove ViewController from display in iOS

  25. 25

    Errno 10048 when trying to read again from network through Bacpypes

  26. 26

    Crash when trying to get Video Stream from IP Camera

  27. 27

    “Unexpected error” when trying to get back Chrome OS from Ubuntu

  28. 28

    ERROR TypeError : _co.userData is undefined In angular 7 when I try to use property binding for value

  29. 29

    When dragging and dropping a file, the dataTransfer.items property is undefined in Firefox and IE, but not Chrome

뜨겁다태그

보관