I'm trying to deepen my knowlade in laravel architecture.
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?)
-
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; }); } }
-
Then i registered it in the providers array in
config/app.php
.'providers' => [ // providers... 'App\Providers\DataFromSearchEngine' ],
-
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?