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 ());});