首页 文章

在Symfony 2.1中使用composer管理供应商时,在哪里注册自动加载?

提问于
浏览
14

我正在使用symfony 2.1,我想向供应商添加一个库 . packagist中不存在该库 . 我无法用作曲家来管理它 . 当我通过composer安装捆绑包或其他供应商时,它会为我管理自动加载 . 但是当供应商不是用作曲家管理时,在哪里注册自动加载?

3 回答

  • 3

    您可以将库添加到不在packagist中的编辑器中 . 您必须将它们添加到 composer.json 文件的 repositories 数组中 .

    这里's how to load a github repository that has a composer.json file, even though it'不在packagist上(例如你要用来修复存储库的一个分支):http://getcomposer.org/doc/02-libraries.md#publishing-to-a-vcs

    在这里's how to load a library that' s在git / svn存储库或zip文件中:http://getcomposer.org/doc/05-repositories.md#types

    使用各种可能性的示例:

    {
      "repositories": [
        {
          "type": "vcs",
          "url": "http://github.com/igorw/monolog"
        },
        {
          "type": "package",
          "package": {
            "name": "smarty/smarty",
            "version": "3.1.7",
            "dist": {
              "url": "http://www.smarty.net/files/Smarty-3.1.7.zip",
              "type": "zip"
            },
            "source": {
              "url": "http://smarty-php.googlecode.com/svn/",
              "type": "svn",
              "reference": "tags/Smarty_3_1_7/distribution/"
            },
            "autoload": {
              "classmap": [
                "libs/"
              ]
            }
          }
        }
      ],
      "require": {
        "monolog/monolog": "dev-bugfix",
        "smarty/smarty": "3.1.*"
      }
    }
    
  • 16

    您应该能够使用Composer注册通过packagist不可用的供应商库 . 我不完全确定,但这应该工作正常:

    {
        "autoload": {
            "psr-0": {
                "Acme": "src/",
                "MyVendorLib": "vendor/my-vendor/src",
                "AnotherLib": "vendor/another-vendor/lib"
            }
        }
    }
    
  • 8

    您只需要修改composer.json文件以获取自动加载值:

    http://getcomposer.org/doc/04-schema.md#autoload

    //composer.json in your symfony 2.1 project
    "autoload": {
         "psr-0": { 
             "": "src/", 
             "YourLibrary": "src/location/of/lib"
         }
    },
    

    然后在你的控制器中例如:

    namespace Acme\UserBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use YourLibrary\FolderName\ClassName;
    
    class DefaultController extends Controller {
    
    /**
     * @Route("/")
     * @Template()
     */
    public function indexAction()
    {
       $lib = new ClassName();
       $lib->getName();
    
       return array('name' => $name);
    }
    
    }
    

相关问题