简体   繁体   中英

Laravel 5 Custom ServiceProvider not found

I'm making a Laravel ServiceProvider for a package. The package is https://github.com/sumocoders/Teamleader

I get the following error

FatalErrorException in ProviderRepository.php line 150: Class 'Notflip\\Teamleader\\TeamleaderServiceProvider' not found

I have no clue what I'm doing wrong, Here's my folder structure

在此处输入图片说明

composer.json in my package

"autoload": {
    "psr-4": {
       "Notflip\\Teamleader": "src/"
    }
}

TeamleaderServiceProvider

<?php namespace Teamleader\Laravel;

use Illuminate\Support\ServiceProvider;

class TeamleaderServiceProvider extends ServiceProvider
{
    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function publishes()
    {
        $this->publishes([
            __DIR__.'/Config/config.php' => config_path('teamleader.php'),
        ]);
    }
    public function register()
    {
        $this->app->bind('Teamleader\Laravel', function () {
            return new Teamleader(config('teamleader.API_GROUP'), config('teamleader.API_SECRET'), config('teamleader.SSL'));
        });
    }
}

Facade

<?php namespace Teamleader\Laravel\Facade;

class Teamleader extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'Teamleader';
    }
}

In my config.php I added the following line to the providers

'Notflip\Teamleader\TeamleaderServiceProvider',

And this line to the aliasses

'Teamleader'=> 'Notflip\Teamleader\Facade\Teamleader'

Anyone has any idea what I might be doing wrong? Thank you! I'm so close to the result!

Your definition in composer is missing the initial slashes and you haven't specified the path to src from root.

"psr-4": {
   "\\Notflip\\Teamleader": "notflip/teamleader-laravel/src/"
}

Also your declaration of the name space at the top of TeamleaderServiceProvider is wrong, it should be:

<?php namespace Notflip\Teamleader;

Solved

In the facade, the IOC binding was named wrong ( wrong case )

The name should have been 'teamleader' in lowercase.

Facade

class Teamleader extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'teamleader';
    }
}

Service Provider

<?php namespace Teamleader\Laravel;

use Illuminate\Support\ServiceProvider;

class TeamleaderServiceProvider extends ServiceProvider
{
    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function publishes()
    {
        $this->publishes([
            __DIR__.'/Config/config.php' => config_path('teamleader.php'),
        ]);
    }
    public function register()
    {
        $this->app->bind('teamleader', function () {
            return new Teamleader(config('teamleader.API_GROUP'), config('teamleader.API_SECRET'), config('teamleader.SSL'));
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM