Basic laravel tutorial-service provider

Source: Internet
Author: User
Tags riak laravel tutorial
Basic laravel tutorial-introduction to service providers

The service provider is the center where laravel applications are started. Your own applications and laravel's core services are all started through service providers.

However, what do we mean by starting? Generally, this means registration, including binding of the registration service, event listening, middleware, and routing. The service provider is the center of application configuration.

If you open the config/app. php file, you will find the providers array. These are the service provider classes that your application will load. Of course, many are delayed loading providers, meaning that not all requests will load these providers, but they will only load when needed.

In this article, you will learn to write your own service provider and register it to the application.

Write service provider

All service providers inherit from the Illuminate \ Support \ ServiceProvider class. This abstract class requires that your provider define at least one register method. In the register method, you should bind only the content to the service container. You never try to register any event listening, routing, or other functions in it.

Artisan CLI can easily generate a new provider using the make: provider command:

php artisan make:provider RiakServiceProvider
Registration method

As mentioned above, in the register method, you should only do one thing, that is, bind the thing to the service container. Do not do other things. Otherwise, the services provided by the provider you are using may not be registered. Now let's look at a basic service provider:

 app->singleton(Connection::class, function ($app) {         return new Connection(config('riak'));     });   }}

This service provider only defines one register method, and uses this method to define a Riak \ Connection implementation in the service container. If you do not understand how the service container works, you can take a look at the service container documentation.

Start method

If we want to register a view composer in the service provider, we should do this in the boot method. The boot method will be executed only after all service providers have completed registration. This means that you have permissions to access all services:

 composer('view', function () {       //     });   }}
Dependency injection of the startup method

You can use the type prompt in the boot method of the service provider to identify the dependency. The service container will automatically inject the required dependencies:

use Illuminate\Contracts\Routing\ResponseFactory;public function boot(ResponseFactory $factory){  $factory->macro('caps', function ($value)){    //  };}
Registration provider

All service providers are registered in the config/app. php configuration file. This file contains the providers array, which lists the names of all service providers. By default, all core services in laravel are registered in this array. These providers start core components in laravel, such as Mail, queue, cache, and others.

You can add the registration provider in the array:

'providers' => [  // Other Service Providers  App\Providers\AppServiceProvider::class,],
Delayed loading provider

If your provider only registers some binding information in the service container, you can postpone the registration so that these services will be registered only when they are actually needed. Delayed registration can improve the performance of your application. Because it will not be loaded by the file system when all requests arrive.

To delay the loading of the provider, you can set the defer attribute to true in the provider class and define a provides method. The provides method returns the binding of the service container registered by the provider:

 app-singleton(Connection::class, function ($app) {        return new Connection($app['config']['riak']);      });    }    /**     * Get the services provided by the provider.     *     * @return array     */     public function provides()     {        return [Connection::class];     }}

Laravel compiles and stores the list of services of all delayed loading service providers and the class names of service providers. Then, laravel loads its service provider only when you try to parse a service.

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.