我已经创建了自己的 composer 包,但是在尝试在包本身之外(即在我的 Laravel 应用程序中)加载和使用它时遇到了一些问题。
我的作曲家文件看起来像:
{
"name": "joelwmale/rss-feed-php",
"type": "library",
"description": "RSS Feeds for PHP is a very small, lightweight, and easy-to-use library for consuming an RSS feed.",
"keywords": [
"rss",
"feed",
"php"
],
"homepage": "https://github.com/joelwmale/rss-feed-php",
"support": {
"issues": "https://github.com/joelwmale/rss-feed-php/issues",
"source": "https://github.com/joelwmale/rss-feed-php"
},
"license": "MIT",
"authors": [
{
"name": "Joel Male",
"email": "[email protected]",
"homepage": "https://www.joelmale.com",
"role": "Author"
}
],
"minimum-stability": "dev",
"require": {
"php": "^7.1",
"ext-simplexml": "*",
"nesbot/carbon": "^1.26.3"
},
"require-dev": {
"phpunit/phpunit": "^7.5",
"phpstan/phpstan": "^0.11.0@dev"
},
"autoload": {
"psr-4": {
"RSSFeedPHP\\": "src/RSSFeedPHP/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"scripts": {
"test": [
"@phpunit",
"@phpcs"
],
"phpunit": "vendor/bin/phpunit",
"phpcs": "php-cs-fixer fix -v --diff --dry-run",
"phpstan": "phpstan analyse --configuration phpstan.neon --level 3 src tests"
},
"extra": {
"laravel": {
"providers": [
"RSSFeedPHP\\Laravel\\ServiceProvider"
]
}
}
}
我的主要课程位于:src/RSSFeedPHP/RSSFeedPHP.php
。但是,当我尝试将它加载到我的 Laravel 应用程序中时:
use joelwmale\RSSFeedPHP;
我得到:
"Class 'joelwmale\RSSFeedPHP' not found"
即使我在我的 Laravel 应用程序中要求它。我可以在 vendor 中看到 joelwmale 文件夹,也可以看到我的包和它的作曲家文件。
composer.json 或我设置项目的方式一定有问题 - 但我无法弄清楚。
有任何想法吗?
编辑:回购链接:https : //github.com/joelwmale/rss-feed-php
"autoload": {
"psr-4": {
"RSSFeedPHP\\": "src/RSSFeedPHP/"
}
},
这将在RSSFeedPHP
命名空间下注册您的类。src/RSSFeedPHP/目录中的任何内容现在都可以像在该命名空间中一样加载。尝试使用
use RSSFeedPHP\RSSFeedPHP;
请注意,RSSFeedPHP
如果您的文件名为RSSFeedPHP.php,则您的类的名称必须是,因此它的命名空间也必须是RSSFeedPHP
,即
<?php
namespace RSSFeedPHP;
class RSSFeedPHP {}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句