My train of thought:
I wrote a tool class, because it is used in many parts of the project, and do not want to use every time to instantiate, so you want to register this tool in Laravel service container. But unfortunately, there are some problems, the following directly affixed to the code and my own ideas, but also please advise where problems arise??? Thank you so much!!!
First step: Register the container first
php artisan make:provider QcloudVideoServiceProvider
The code is as follows:
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']; }}
Step Two: Register facades
Under the app directory, create a foundation/facades/qcloudfacades.php with the following code:
第三步:注册Service
在config/app.php中注册Service Provider 和 Facades
providers => [ //... App\Providers\QcloudVideoServiceProvider::class,]aliases => [ //... 'QCvod' => App\Foundation\Facades\QCvodFacades::class,]
第四步:调用
dd(ACvod::getRegion());
getRegion()是工具类中的一个getter方法
问题出现了:
Class qcloud.vod does not exist
非常感谢您耐心看完!非常感谢!
求解?
Reply content:
My train of thought:
I wrote a tool class, because it is used in many parts of the project, and do not want to use every time to instantiate, so you want to register this tool in Laravel service container. But unfortunately, there are some problems, the following directly affixed to the code and my own ideas, but also please advise where problems arise??? Thank you so much!!!
First step: Register the container first
php artisan make:provider QcloudVideoServiceProvider
The code is as follows:
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']; }}
Step Two: Register facades
Under the app directory, create a foundation/facades/qcloudfacades.php with the following code:
第三步:注册Service
在config/app.php中注册Service Provider 和 Facades
providers => [ //... App\Providers\QcloudVideoServiceProvider::class,]aliases => [ //... 'QCvod' => App\Foundation\Facades\QCvodFacades::class,]
第四步:调用
dd(ACvod::getRegion());
getRegion()是工具类中的一个getter方法
问题出现了:
Class qcloud.vod does not exist
非常感谢您耐心看完!非常感谢!
求解?
QcloudVideoServiceProvider
处
$this->app->singleton('qcloud.vod', function ($app) { return new QCVod(config('QCLOUD_KEY'), config('QCLOUD_SECRET'));});
应该是这样了。
暂时没写过“服务提供者”和“门面”,感觉是singleton()
这里要写全命名空间。
如果仅仅是作为工具类,请参考 laravel 辅助函数
composer dump-autoload