我正在尝试设置一个composer包,但当我从一个新项目中尝试它时,似乎无法加载它
我的包在这里https://github.com/shorif2000/pagination,包架师在这里https://packagist.org/packages/shorif2000/pagination
在一个新项目中,我有
{
"name": "ec2-user/pagination",
"authors": [
{
"name": "shorif2000",
"email": "shorif2000@gmail.com"
}
],
"require": {
"shorif2000/pagination": "dev-master"
},
"minimum-stability" : "dev"
}
$ cat index.php
<?php
require './vendor/autoload.php';
use Pagination\Paginator;
$totalItems = 1000;
$itemsPerPage = 50;
$currentPage = 8;
$urlPattern = '/foo/page/(:num)';
$paginator = new Paginator($totalItems, $itemsPerPage, $currentPage, $urlPattern);
?>
<html>
<head>
<!-- The default, built-in template supports the Twitter Bootstrap pagination styles. -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<?php
// Example of rendering the pagination control with the built-in template.
// See below for information about using other templates or custom rendering.
echo $paginator;
?>
</body>
</html>它会失败,并显示错误Fatal error: Uncaught Error: Class 'Pagination\Paginator' not found in /opt/pagination/index.php:12 Stack trace: #0 {main} thrown in /opt/pagination/index.php on line 12。我尝试了use shorif2000\Pagination\Paginator;,它也给出了同样的错误
发布于 2019-11-02 00:24:50
这里有不止一个问题。
composer.json (软件包)
在您的编写器文件(用于分页库)中,将PSR-0更改为PSR-4。PSR-0是一种旧格式,大约5年前(2014年)就被弃用了。
您还应该始终以\\结束命名空间。所以包应该是:
"autoload" : {
"psr-4" : {
"Pagination\\" : "src/"
}
},Read more about composer autoload here
命名空间
因为您的名称空间是Pagination\,所以应该在使用它的代码中使用该名称空间。
所以如果你有一个类:
namespace Pagination;
class Pagination {
...
}那么你的use语句应该是:
use Pagination\Pagination;Read more about PHP namespaces here
shorif2000是供应商名称(仅供composer根据供应商名称对软件包进行分组,并消除不同软件包相互覆盖的风险。
https://stackoverflow.com/questions/58662398
复制相似问题