Laravel Basic Tutorial-service provider

Source: Internet
Author: User
Tags riak

Brief introduction

The service provider is the center of the Laravel application launch. Your own application and the core services of Laravel are initiated by the service provider.

But what do we mean by start-up? Typically, this means registering, including bindings for registration services, event snooping, middleware, and routing. The service provider is the center of the application configuration.

If you open the config/app.php file, you will find the providers array. These are the service provider classes that your app will load. Of course, many are providers of deferred loading, meaning that not all requests will be loaded by the provider, but only when needed.

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

Authoring Service Providers

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

The Artisan CLI can easily generate a new provider with the Make:provider command:

PHP Artisan Make:provider Riakserviceprovider

Registration method

As mentioned earlier, in the Register method, you should only do one thing, that is, binding things into the service container. Don't do anything else. Otherwise, you may be using a service provided by the provider that has not been registered. Now, let's look at one of the most basic service providers:

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

The service provider simply defines a register method and uses this method to define a riak\connection implementation in the service container. If you don't understand how the service container works, you can look at the documentation for the service container.

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 not be executed until all service providers have been registered. This means that you have access to all services:

 
  Composer (' View ', function () {       //     });}   }

Dependency injection of the startup method

You can use type hints to identify dependencies in the service provider's boot method. The service container will automatically inject the required dependencies in:

Use Illuminate\contracts\routing\responsefactory;public function boot (responsefactory $factory) {  $factory Macro (' Caps ', function ($value)) {    //  };}

Registered 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, the core services in Laravel are registered in this array. These providers launch core components in Laravel, such as mail, queues, caches, and others.

You can add a registration provider in the array:

' Providers ' = [  //other Service providers  App\providers\appserviceprovider::class,],

Deferred load Provider

If your provider only registers some binding information in the service container, you can choose to defer registration so that the services are registered only when they are actually needed. Delaying registration can improve the performance of your app. Because it does not load through the file system when all requests arrive.

In order to postpone the loading of the provider, you can set the defer property to true in the provider class and define a provides method. The provides method returns the bindings for 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 service list of all deferred-loaded service providers and the class name of the service provider. Laravel will then load its service provider only if you try to parse one of those services.

  • 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.