1

I want to put some code that I shall use in different projects outside the app's bundles. I put it in a new folder in the vendor folder but I can't included in the project. Which are the steps?

  • Should I follow a special structure of folder and namespaces
  • How do I included in my project.

On Google I found a lot of ambiguous answers. I tried also this but it did not work.

Community
  • 1
  • 1
catalinux
  • 1,462
  • 14
  • 26

1 Answers1

2

You need to get your library loaded. You do this by telling composer where to find your library.

The best thing would be to set up a git repository for this source code and then use composer to load it, keep it up to date and include it into the autoloader. You may need to get used to composer to do this, but as composer is getting the default dependency management tool for php, the time is not wasted. You can find the docs here: http://getcomposer.org/doc/

If you don't want to do this, you can add a new src code folder to composer. Let's say your folder structure is like this:

symfony2/ vendor/ yourlibrary/ Bla/ Blub/ MyClass.php

First, the MyClass.php should have the namespace Bla/Blub defined. Else Then you can add your library like this to the composer.json file:

"autoload": {
    "psr-0": { 
        "": "src/",
        "Bla": "vendor/yourlibrary/"
    }
}

You already have autoload defined, so you need to overwrite it!

If your library doesn't have namespaces, you can define composer to load it nonetheless. I am not using this, so I can just link the docs where you can read it up: http://getcomposer.org/doc/04-schema.md#autoload

Sgoettschkes
  • 13,141
  • 5
  • 60
  • 78
  • After I modify composer.json do I have to call some command script? Do I have do modify an other script ? ( I am also confused about the existence of autoload.php or autoloader.php) – catalinux Nov 03 '12 at 08:22
  • You should run `composer.phar install` which will update the autoload.php. There should be only one autoload.php file in your vendors folder, nowhere else. – Sgoettschkes Nov 03 '12 at 12:48