Add your own class to the IOC container

Source: Internet
Author: User
Add your own classes to the IOC container as you know in the previous article that serviceprovider manages container. Therefore, you must register serviceprovider to manage your classes when adding your own classes to the container.

1. create a custom class

It should be noted that laravel can automatically load php classes in its app directory, as long as it complies with the naming rules of psr4.

App/Billing/Stripe. php // here, a Billing directory is created under the app, and a Stripe class is put in it. <? Phpnamespace App \ Billing; // The namespace is required for php to correctly find the class Stripe {public function charge () {dd ('charge ');}}
2. create a service provider

Use this command to create

php artisan make:provider BillingServiceProvider
App/Providers/BillingServiceProvider. php // after successful creation, the php file is generated in this directory.
 App-> bind ('billing', function () {// here the bind method is used to create a INER and bind a keyword of billing for association, this container returns a Stripe instance, which is equivalent to instantiation. Return new Stripe ();});}}
3. Register service provider

This service provider can be recognized by laravel only after it is registered in app. php, and can be called in the code.

Config/app. php 'Providers '=> [// add/** Laravel Framework Service providers in the providers array... */Illuminate \ Auth \ AuthServiceProvider: class, Illuminate \ Broadcasting \ BroadcastServiceProvider: class, Illuminate \ Bus \ BusServiceProvider: class, Illuminate \ Cache \ CacheServiceProvider: class, illuminate \ Foundation \ Providers \ lelesuppserviceserviceprovider: class, Illuminate \ Cookie \ response: class, Illuminate \ Database \ DatabaseServiceProvider: class, Illuminate \ Encryption \ EncryptionServiceProvider: class, illuminate \ Filesystem \ FilesystemServiceProvider: class, Illuminate \ Foundation \ Providers \ FoundationServiceProvider: class, Illuminate \ Hashing \ HashServiceProvider: class, Illuminate \ Mail \ MailServiceProvider: class, illuminate \ Pagination \ PaginationServiceProvider: class, Illuminate \ Pipeline: class, Illuminate \ Queue \ QueueServiceProvider: class, Illuminate \ Redis \ RedisServiceProvider: class, illuminate \ Auth \ Passwords \ PasswordResetServiceProvider: class, Illuminate \ Session \ SessionServiceProvider: class, Illuminate \ Translation \ TranslationServiceProvider: class, Illuminate \ Validation \ ValidationServiceProvider: class, illuminate \ View \ ViewServiceProvider: class, Collective \ Html \ HtmlServiceProvider: class, 'maatwebsite \ Excel \ excelserviceprovider',/** Application Service Providers... */App \ Providers \ AppServiceProvider: class, App \ Providers \ AuthServiceProvider: class, App \ Providers \ EventServiceProvider: class, App \ Providers \ RouteServiceProvider: class, app \ Providers \ BillingServiceProvider: class, // add according to the format],
4. manage routes (test)

This is mainly for the purpose of testing, so you can directly test the service provider in the routing.

App/Http/routes. php // There are two ways to use it. The first is related to the two container usage methods mentioned in the previous article, and the second is to implement the automatic parsing mechanism. // Route :: get ('/', function () {// $ billing = app ('billing'); // you can directly input keywords using make () // dd ($ billing-> charge (); //}); Route: get ('/', function (App \ Billing \ Stripe $ billing) {// input a class name and a variable. laravel will automatically search for and parse dd ($ billing-> charge ());});

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.