简体   繁体   中英

Laravel package auto-discovery not finding Provider

I'm creating my first package for Laravel, and I'm having trouble installing it correctly via composer. It's using the Laravel (v5.5+) auto-discovery via the package's composer.json , but when it runs the artisan package:discover command, it can't find my custom ServiceProvider. This is my composer.json :

{
    "name": "mpemburn/api-consumer",
    "description": "Configurable consumer for RESTful API's",
    "type": "library",
    "keywords": ["api","restful","laravel"],
    "license": "MIT",
    "authors": [
        {
            "name": "Mark Pemburn",
            "email": "mark@pemburn.com"
        }
    ],
    "autoload": {
        "psr-4": {
            "Mpemburn\\ApiConsumer\\": "src/"
        }
    },
    "require": {
        "php": ">=7.4"
    },
    "extra": {
        "branch-alias": {
            "dev-master": "1.0-dev"
        },
        "laravel": {
            "providers": [
                "Mpemburn\\ApiConsumer\\ApiConsumerProvider"
            ]
        }
    },
    "minimum-stability": "dev"
}

...and this is ApiConsumerProvider :

<?php

namespace Mpemburn\ApiConsumer;

use Illuminate\Support\ServiceProvider;
use Mpemburn\ApiConsumer\Handlers\ResponseHandler;
use Mpemburn\ApiConsumer\Interfaces\ResponseHandlerInterface;

class ApiConsumerProvider extends ServiceProvider
{
    public function register(): void
    {
        // Bind ResponseHandlerInterface class(es)
        app()->bind(ResponseHandlerInterface::class, ResponseHandler::class);
    }

    public function boot(): void
    {
        $this->publishes([
            __DIR__.'/../config/api-consumer.php' => config_path('api-consumer.php'),
        ], 'config');
    }
}

I've scrutinized this six ways from Sunday, and I cannot figure out what I'm doing wrong.

You can find the package here: https://github.com/mpemburn/api-consumer

Thanks in advance for any help you can offer!

Edited to add: composer version 2.0.6, Laravel version 8.16.0

Had a look at the github link - problem is that your service provider class file is not in the src directory

Move the ApiConsumerProvider.php inside the src directory then it should work.

Your psr4 autoload key points to the src directory for the Mpemburn\ApiConsumer namespace.

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