Ad

Package:discovery Does Not Find My ServiceProvider

- 1 answer

I have created a custom Laravel package. Its hosted on a private repository.

When I try to install the package in a Laravel application like this:

composer require memberportal/congress-models

I get this error:

In ProviderRepository.php line 208:

  Class 'Memberportal\CongressesModels\CMServiceProvider' not found  


Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1

Installation failed, reverting ./composer.json to its original content.

I have double checked everything a hundred times, but I am clueless why he doesn't find the class CMServiceProvider.php.

Do I need to specify the provider anywhere else then in the composer.json?

This is the content of composer.json from my package:

{
    "name": "memberportal/congress-models",
    "description": "Takes care of the models between API and client",
    "type": "metapackage",
    "authors": [
        {
            "name": "My name",
            "email": "[email protected]"
        }
    ],
    "version" : "1.0",
    "minimum-stability": "stable",
    "require": {
        "laracasts/presenter": "^0.2.2"
    },
    "require-dev": {
        "orchestra/testbench": "^4.0"
    },
    "autoload": {
        "psr-4": {
          "Memberportal\\CongressesModels\\": "src"
        }
    },
    "autoload-dev": {
      "psr-4": {
          "Memberportal\\CongressesModels\\Tests\\": "tests"
        }
    },
    "extra": {
        "laravel": {
            "providers": [
                "Memberportal\\CongressesModels\\CMServiceProvider"
            ]
        }
    }
}

This is the folder structure:

enter image description here

This is the content of CMServiceProvider.php:

<?php

namespace Memberportal\CongressesModels;

use Illuminate\Support\ServiceProvider;

class CMServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        $this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
    }
}
Ad

Answer

Since you want to install a Laravel package your package type should be package. A metapackage is a package that does not contain actual software, it simply depends on other packages to be installed.

In short, change

"type": "metapackage",

to

"type": "package",

and Laravel will find the service provider with auto-discovery when the package is required by composer.

Ad
source: stackoverflow.com
Ad