首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用packagist设置composer包

如何使用packagist设置composer包
EN

Stack Overflow用户
提问于 2019-11-02 00:08:10
回答 1查看 57关注 0票数 0

我正在尝试设置一个composer包,但当我从一个新项目中尝试它时,似乎无法加载它

我的包在这里https://github.com/shorif2000/pagination,包架师在这里https://packagist.org/packages/shorif2000/pagination

在一个新项目中,我有

代码语言:javascript
复制
{
    "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;,它也给出了同样的错误

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-02 00:24:50

这里有不止一个问题。

composer.json (软件包)

在您的编写器文件(用于分页库)中,将PSR-0更改为PSR-4PSR-0是一种旧格式,大约5年前(2014年)就被弃用了。

Read more about PSR-4 here

您还应该始终以\\结束命名空间。所以包应该是:

代码语言:javascript
复制
"autoload" : {
    "psr-4" : {
        "Pagination\\" : "src/"
    }
},

Read more about composer autoload here

命名空间

因为您的名称空间是Pagination\,所以应该在使用它的代码中使用该名称空间。

所以如果你有一个类:

代码语言:javascript
复制
namespace Pagination;

class Pagination {
    ...
}

那么你的use语句应该是:

代码语言:javascript
复制
use Pagination\Pagination;

Read more about PHP namespaces here

shorif2000是供应商名称(仅供composer根据供应商名称对软件包进行分组,并消除不同软件包相互覆盖的风险。

Read more about composer vendor name here

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58662398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档