Laravel Service Providers issue

WBOY
Release: 2016-08-04 09:20:32
Original
1855 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

Step 4: Call

<code>dd(ACvod::getRegion());</code>
Copy after login
Copy after login

getRegion() is a getter method in the tool class

The question arises:

<code>Class qcloud.vod does not exist</code>
Copy after login
Copy after login

Thank you very much for your patience in reading! Thank you so much!

Need the solution?

Reply content:

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

Step 4: Call

<code>dd(ACvod::getRegion());</code>
Copy after login
Copy after login

getRegion() is a getter method in the tool class

The question arises:

<code>Class qcloud.vod does not exist</code>
Copy after login
Copy after login

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>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!