As you know from the previous article, ServiceProvider is managing container, so when you add your own classes to container, you also register serviceprovider to manage
1. Create a custom class
It is important to note that Laravel can automatically load the PHP classes in its own app directory, as long as it conforms to the PSR4 naming convention.
app/billing/stripe.php //Here in the APP set up a billing directory, which put a Stripe class <? phpnamespace app\billing; A namespace is required to be correctly searched by PHP to 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 //Create a PHP file in this directory after successful creation
app->bind (' Billing ', function () { //Here is a bind method to create a container, bound to a billing keyword to associate, this container will return a stripe instance, the equivalent of instantiation. return new Stripe ();} );} }
3. Register Service Provider
Registered in the app.php can be laravel to identify the service provider, in order to call in the code
config/app.php ' providers ' = + [/////* * * * * * Laravel Framework Service providers in providers array ... */Illuminate\auth\authserviceprovider::class, Illuminate\broadcasting\broadcastserviceprovider::class, Illuminate\bus\busserviceprovider::class, Illuminate\cache\cacheserviceprovider::class, Illuminate\Found Ation\providers\consolesupportserviceprovider::class, Illuminate\cookie\cookieserviceprovider::class, Illumi Nate\database\databaseserviceprovider::class, Illuminate\encryption\encryptionserviceprovider::class, Illumi Nate\filesystem\filesystemserviceprovider::class, Illuminate\foundation\providers\foundationserviceprovider:: Class, Illuminate\hashing\hashserviceprovider::class, Illuminate\mail\mailserviceprovider::class, Ill Uminate\pagination\paginationserviceprovider::class, Illuminate\pipeline\pipelineserviceprovider::class, Ill Uminate\queue\queueserviceprovIder::class, Illuminate\redis\redisserviceprovider::class, Illuminate\auth\passwords\passwordresetservicepro Vider::class, Illuminate\session\sessionserviceprovider::class, Illuminate\translation\translationservicepro Vider::class, Illuminate\validation\validationserviceprovider::class, Illuminate\view\viewserviceprovider::c Lass, Collective\html\htmlserviceprovider::class, ' maatwebsite\excel\excelserviceprovider ',/* * Application Service Providers ... */App\providers\appserviceprovider::class, app\providers\auths Erviceprovider::class, App\providers\eventserviceprovider::class, App\providers\routeserviceprovider::class, App\providers\billingserviceprovider::class,//Add by format],
4. Managing Routing (testing)
Mainly for testing purposes, so directly in the routing to test service provider
app/http/routes.php//There are two ways to use this, the first is related to the two container used in the previous article, and the second is to implement the//route::get ('/', function () {//) through the automatic parsing mechanism. $billing = App (' billing '); You can also use make ()// DD ($billing->charge ()) to pass in keywords directly,//}); Route::get ('/', function (App\billing\stripe $billing) { //pass in a class name and a variable, Laravel will automatically find and parse DD ($billing- >charge ());});