My thoughts:
I wrote a tool class myself. Since it will be used in multiple places in the project, and I don’t want to instantiate it every time I use it, I want to register this tool in Laravel’s Service Container. But unfortunately there are some problems. I will paste the code and my own ideas directly below. Please tell me where the problem is? ? ? Thank you so much! ! !
Step one: Register the container first
<code>php artisan make:provider QcloudVideoServiceProvider</code>
The code is as follows:
<code><?php namespace App\Providers; use App\Xiaoteng\QCVod; use Illuminate\Support\ServiceProvider; class QcloudVideoServiceProvider extends ServiceProvider { /** * Indicates if loading of the provider is deferred. * * @var bool */ protected $defer = true; /** * Register the application services. * * @return void */ public function register() { $this->app->singleton('qcloud.vod', function ($app) { $glass = new QCVod(config('QCLOUD_KEY'), config('QCLOUD_SECRET')); return $glass->setRegion('gz'); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return ['qcloud.vod']; } } </code>
Step 2: Register for Facades
Create Foundation/Facades/QcloudFacades.php under the app directory, the code is as follows:
<code><?php namespace App\Foundation\Facades; use Illuminate\Support\Facades\Facade; class QCvodFacades extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'qcloud.vod'; } }</code>
Step 3: Register Service
Register Service Provider and Facades in config/app.php
<code>providers => [ //... App\Providers\QcloudVideoServiceProvider::class, ] aliases => [ //... 'QCvod' => App\Foundation\Facades\QCvodFacades::class, ]</code>
Step 4: Call
<code>dd(ACvod::getRegion());</code>
getRegion() is a getter method in the tool class
The question arises:
<code>Class qcloud.vod does not exist</code>
Thank you very much for your patience in reading! Thank you so much!
Need the solution?
My thoughts:
I wrote a tool class myself. Since it will be used in multiple places in the project, and I don’t want to instantiate it every time I use it, I want to register this tool in Laravel’s Service Container. But unfortunately there are some problems. I will paste the code and my own ideas directly below. Please tell me where the problem is? ? ? Thank you so much! ! !
Step one: Register the container first
<code>php artisan make:provider QcloudVideoServiceProvider</code>
The code is as follows:
<code><?php namespace App\Providers; use App\Xiaoteng\QCVod; use Illuminate\Support\ServiceProvider; class QcloudVideoServiceProvider extends ServiceProvider { /** * Indicates if loading of the provider is deferred. * * @var bool */ protected $defer = true; /** * Register the application services. * * @return void */ public function register() { $this->app->singleton('qcloud.vod', function ($app) { $glass = new QCVod(config('QCLOUD_KEY'), config('QCLOUD_SECRET')); return $glass->setRegion('gz'); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return ['qcloud.vod']; } } </code>
Step 2: Register for Facades
Create Foundation/Facades/QcloudFacades.php under the app directory, the code is as follows:
<code><?php namespace App\Foundation\Facades; use Illuminate\Support\Facades\Facade; class QCvodFacades extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'qcloud.vod'; } }</code>
Step 3: Register Service
Register Service Provider and Facades in config/app.php
<code>providers => [ //... App\Providers\QcloudVideoServiceProvider::class, ] aliases => [ //... 'QCvod' => App\Foundation\Facades\QCvodFacades::class, ]</code>
Step 4: Call
<code>dd(ACvod::getRegion());</code>
getRegion() is a getter method in the tool class
The question arises:
<code>Class qcloud.vod does not exist</code>
Thank you very much for your patience in reading! Thank you so much!
Need the solution?
QcloudVideoServiceProvider
at
<code>$this->app->singleton('qcloud.vod', function ($app) { return new QCVod(config('QCLOUD_KEY'), config('QCLOUD_SECRET')); });</code>
That should be it.
I haven’t written “service provider” and “facade” yet. I feel like singleton()
should write the full namespace here.
If it is only used as a tool class, please refer to laravel auxiliary function
composer dump-autoload