Laravel5 (Lumen) creates a service provider ServiceProviders

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.