Laravel5 (Lumen) creates a service provider ServiceProviders. when launching Sina SAE, Lumen is installed. to implement Memcahce and Storage, Lumen's service provider is used, after reading the official documents and reading some documents, record them. (In Lumen, Laravel achieves almost the same purpose)
1. create a new Models folder under the app \ Http \ folder, create the Example. php file under the Models folder, and add the app \ Http \ Models to the autoload class in the composer. json file for automatic loading.
"autoload": { "psr-4": { "App\\": "app/", "App\\Http\\Models\\": "app/Http/Models/" } },
Create an Example class in the example. php file
2. create a service provider ExampleServiceProvider. php in the app \ Providers \ folder. And register the example class in Models.
App-> singleton ('app \ Http \ Models \ Example ', function ($ App) {return new Example ($ app );});}} 3. add the Example service provider to start and add it to bootstrap \ app. php.
$app->register(App\Providers\ExampleServiceProvider::class);
After completing the preceding operations, our Example service provider has been added and enabled. we will use it below.
4. use it in Controller
For testing, we created a file named example and ExampleController named ExampleController. php.
Example = $ example;} public function index () {$ this-> example-> sayHello ();} 5. execute the route in the browser. the following output is displayed:
hello,this is a model say
Summary:
1. Laravel's service provider is very convenient and core to add various extensions to you. It is very convenient to extend functions for you just like adding components of Yii. Laravel's service provider is actually a complicated thing. here it is just a simple instance. For more information, please refer to official documents and instances.
2. Laravel's extended development is also started by the service provider, for example:
Cache::extend('mongo', function($app){ return Cache::repository(new MongoStore);}); The following are original Chinese translation documents:
Cache: extend call can be completed in the boot method of App \ Providers \ AppServiceProvider attached to the new Laravel application by default, or you can set up your own service provider to place this extension.
Reference:
1. http://www.golaravel.com/laravel/docs/5.0/providers/
2. https://laravel.com/docs/5.2/providers