简体   繁体   中英

Laravel CustomServiceProvider Class not found in package when required

I am having issues with a custom composer package I created to work for Laravel. I followed all the necessary steps others have said. First require the package, then composer-dumpautoload -o , and then add ServiceProvider to config/app.php . However, laravel cannot find the package ServiceProvider. Maybe my name spacing is off? Here is my code below along with the link to my github repo

https://github.com/InspiredByKeith/forecast-package

config/app.php

KeithRoye\Forecast\WeatherServiceProvider::class,

WeatherServiceProvider.php

 <?php

namespace KeithRoye\Forecast;

use Illuminate\Support\ServiceProvider;

class WeatherServiceProvider extends ServiceProvider

{
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        $this->loadRoutesFrom(__DIR__.'/routes/web.php');
    }
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Composer.json of package

{
    "name": "keithroye/forecast",
    "description": "This package will allow users to obtain a 5 day weather forecast based upon the given zip code provided.",
    "type": "library",
    "require-dev": {
        "guzzlehttp/guzzle": "^6.3@dev"
    },
    "autoload-dev": {
        "psr-4": {
            "KeithRoye\\Forecast": "src/"
        }
    },

    "license": "MIT",
    "authors": [
        {
            "name": "Keith Roye",
            "email": "inspiredbykeith@gmail.com"
        }
    ],
    "minimum-stability": "dev"
}

Composer.json of Application

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": "^7.1.3",
        "fideloper/proxy": "^4.0",
        "keithroye/forecast": "dev-master",
        "laravel/framework": "5.7.*",
        "laravel/tinker": "^1.0"
    },
    "require-dev": {
        "beyondcode/laravel-dump-server": "^1.0",
        "filp/whoops": "^2.0",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^2.0",
        "phpunit/phpunit": "^7.0"
    },
    "autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "extra": {
        "laravel": {
            "dont-discover": [
            ]
        }
    },
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ],
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

Because you are using autoload-dev => replace it with autoload (and do the same to require-dev => require ).

The documentation of composer explains it, but I understand it's not clear if you're not familiar with the terminology.

https://getcomposer.org/doc/04-schema.md#autoload-dev

autoload-dev (root-only) …

And the documentation for "root-only" ie Root Package :

Certain fields only apply when in the root package context.

I think example given there is quite good though:

A package can be the root package or not, depending on the context. For example, if your project depends on the monolog library, your project is the root package. However, if you clone monolog from GitHub in order to fix a bug in it, then monolog is the root package.

Or to put it in another way

autoload-dev is only relevant when developing your package. Usually you define your tests there which are commonly put next to src/ in tests/ (on the same top-level) and thus they need a separate directive for that.

Replace autoload-dev with autoload and backslashes after Forecast in below section in composer.json.

"autoload-dev": {
    "psr-4": {
        "KeithRoye\\Forecast\\": "src/"
    }
}

After edit this, run composer dump-autoload or composer dumpautoload and check it again.

You must include a leading backslash in the composer psr-4 definition namespace:

"autoload-dev": {
    "psr-4": {
        "KeithRoye\\Forecast\\": "src/"
    }
},

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