Where to register autoload when the vendor is not managed with composer in Symfony 2.1?

孤人 提交于 2019-12-31 12:56:52

问题


I'm using symfony 2.1 and I want to add a library to vendors. The library do not exists in packagist. I can't manage it with composer. When I install bundles or others vendors through composer, it manage autoload for me. But where to register autoload when the vendor is not managed with composer?


回答1:


You can add libraries to composer that are not in packagist. You must add them in the repositories array of your composer.json file.

Here's how to load a github repository that has a composer.json file, even though it's not on packagist (for example a fork you would have done to fix a repository) : http://getcomposer.org/doc/02-libraries.md#publishing-to-a-vcs

And here's how to load a library that's on a git/svn repository, or a zip file : http://getcomposer.org/doc/05-repositories.md#types

An example using various possibilities:

{
  "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.*"
  }
}



回答2:


You should be able to use Composer for registering vendor libraries not available via packagist. I'm not entirely sure, but this should work fine:

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



回答3:


You just need to modify your composer.json file for the autoload value:

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"
     }
},

And then in your controller for example:


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);
}

}



来源:https://stackoverflow.com/questions/11496255/where-to-register-autoload-when-the-vendor-is-not-managed-with-composer-in-symfo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!