我在为Laravel创建的自定义作曲家软件包中遇到问题。我遵循了别人所说的所有必要步骤。首先需要该软件包,然后是composer-dumpautoload -o
,然后将ServiceProvider
添加到config/app.php
。但是,laravel无法找到软件包ServiceProvider。也许我的名字间距不对了?这是下面的代码以及指向我的github存储库的链接
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
{
"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
{
"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
}
答案 0 :(得分:2)
因为您使用的是autoload-dev
=>替换为autoload
(并对require-dev
=> require
进行相同操作)。
作曲家的文档对此进行了解释,但我了解您是否不熟悉该术语尚不清楚。
https://getcomposer.org/doc/04-schema.md#autoload-dev
autoload-dev(仅root用户) …
还有“仅根目录”的文档,即Root Package:
某些字段仅在根包上下文中适用。
我认为给出的例子还是很不错的:
一个包可以是根包,也可以不是根包,具体取决于上下文。例如,如果您的项目依赖于monolog库,则您的项目是根包。但是,如果您从GitHub克隆monolog来修复其中的错误,则monolog是根软件包。
autoload-dev
仅在开发您的软件包时才相关。通常,您在此处定义测试,这些测试通常将 next 放在src/
中的tests/
中(位于同一顶层),因此它们需要单独的指令。>
答案 1 :(得分:1)
在composer.json的以下部分中,将autoload-dev
替换为autoload
,并在Forecast
之后加反斜杠。
"autoload-dev": {
"psr-4": {
"KeithRoye\\Forecast\\": "src/"
}
}
修改后,运行composer dump-autoload
或composer dumpautoload
并再次检查。
答案 2 :(得分:-1)
您必须在composer psr-4定义名称空间中包含一个反斜杠:
"autoload-dev": {
"psr-4": {
"KeithRoye\\Forecast\\": "src/"
}
},