php - 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) …</p>
And the documentation for "root-only" i.e. 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.
推荐阅读
- python - 在 python 中用于打印格式时无法打印字典值
- c++ - 在 wxWidgets 中将字符串存储为 wstring 或 wxString?
- java - Facebook Android SDK:“无法解析符号 EVENT_NAME_SUBSCRIBE”
- ssis - 在 ADO 网络源的 SQL 命令 SSIS 中使用变量
- python - 我怎样才能让这个二分搜索工作?
- python - 如何使用 python csv 或 pandas 模块水平组合多个 CSV 文件?
- python - 在 python 中的 Wxpython 中第二次打印时出错
- django - Django 添加第二个静态文件 URL
- python - 在python中将类返回给自身
- html - 如何仅隐藏左侧滑块的溢出?