Laravel Service Providers Issues

Source: Internet
Author: User
Tags aliases
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

  • 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.