简体   繁体   English

如何使用 packagist 设置作曲家 package

[英]how to set up composer package with packagist

I am trying to set up a composer package but i cannot seem to get it to load when i try it from a new project我正在尝试设置一个作曲家 package 但是当我从一个新项目中尝试它时我似乎无法加载它

my package is here https://github.com/shorif2000/pagination and the packgist is here https://packagist.org/packages/shorif2000/pagination我的 package 在这里https://github.com/shorif2000/pagination和包装师在这里Z5E056C500A1C4B6A7110B50D80D807BADE5Z/packagist/

in a new project i have在我的一个新项目中

{
    "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>

it fails with error 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 . it fails with error 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 . I tried use shorif2000\Pagination\Paginator;我尝试use shorif2000\Pagination\Paginator; which gave same error as well这也给出了同样的错误

There's more than one issue here.这里的问题不止一个。

composer.json (package) composer.json(包)

In your composer file (for the Pagination library), change PSR-0 to PSR-4 .在您的作曲家文件(用于分页库)中,将PSR-0更改为PSR-4 PSR-0 is an old format that was deprecated around 5 years ago (2014). PSR-0是一种旧格式,大约在 5 年前(2014 年)被弃用。

Read more about PSR-4 here在此处阅读有关 PSR-4 的更多信息

You should also always end the namespace with \\ .您还应该始终以\\结束命名空间。 So the package should be:所以 package 应该是:

"autoload" : {
    "psr-4" : {
        "Pagination\\" : "src/"
    }
},

Read more about composer autoload here在此处阅读有关作曲家自动加载的更多信息

Namespaces命名空间

Since you're namespace is Pagination\ , that's the namespace you should use in the code that uses it.由于您的命名空间是Pagination\ ,因此您应该在使用它的代码中使用该命名空间。

So if you have a class with:因此,如果您有一个 class :

namespace Pagination;

class Pagination {
    ...
}

then your use statement should simply be:那么你的use语句应该是:

use Pagination\Pagination;

Read more about PHP namespaces here在此处阅读有关 PHP 命名空间的更多信息

The shorif2000 is the vendor name (which is only for composer to be able to group packages on vendor name and to remove the risk of different packages overwriting each other. shorif2000是供应商名称(仅用于作曲家能够根据供应商名称对包进行分组并消除不同包相互覆盖的风险。

Read more about composer vendor name here在此处阅读有关作曲家供应商名称的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM