So I found the right syntax... It will help some others
$loader->add('seculibs\\collections',__DIR__ . '/../vendor');
Question
I asked a similar questions before but had no right answers. Maybe I did not explain good.
I have 2 classes of my own that I would like to add in symfony project.
seculibs/collections/A.php
and seculibs/collections/B.php
(it is not on github, it is local).
I would like to add my seculibs folder in the vendor folder and ask symfony to load the classes.
Obviously everything happens in app/autoload.php
I thought that doing this would be okay but not :
$loader->add('seculibs/collections',__DIR__ . '/../vendor/seculibs/collections/');
it still cannot find the classes.
Can somebody tell me exactly what to do? I am with Symfony 2.3.6
Please do not refer to composer.json
neither to the UniversalClassLoader
and its registerNamespaces
method as this is not the class I am dealing with in this autoload.php
Solution
So I found the right syntax... It will help some others
$loader->add('seculibs\\collections',__DIR__ . '/../vendor');
OTHER TIPS
Do you HAVE to autoload the classes? I have used my own classes by simply giving them a namespace in my project, and then including them with a use
statement in the file that I would like to use them in. For example, I have a class that I put in a custom location within my project, src/My/AppBundle/Utils/MyClass.php
, and within the MyClass.php
file, I gave the namespace My/AppBundle/Utils
Now in a controller, I include that class with a normal use
statement.
use My/AppBundle/Utils/MyClass.php
// .....
class MyController extends Controller {
// ....
}
This way I can use my class anywhere in my application by referencing the namespace. I know this approach is different from what you may have expected, but it works great.
UPDATE:
It doesn't matter where you put the files. If you want them in the vendor folder, put them there, and give them a namespace that makes sense.
So maybe you have something like this:
// in vendors/MyUtils/Classes/MyClass.php
namespace MyUtils/Classes
class MyClass {
// ....
}
And then you can still include the class with a use
statement like I mentioned.
Look at vendor/composer/ClassLoader.php You can see that the add method expects a prefix as the first argument. You can leave it blank if your classes have no namespace.
Assuming your classes are not namespaced then use:
$loader->add(null,__DIR__ . '/../vendor/seculibs/collections/');
$a = new \A();
If they do have a namespace then just pass the prefix of your namespace to add.
$loader->add('Cerad',__DIR__ . '/../vendor/seculibs/collections/');
namespace Cerad\Collections;
class A {}
$a = new Cerad\Collections\A();
Of course you also have to have the Cerad\Collections directory structure under your vendor directory.
==============================================
Update
If you have in /vendor/seculibs/collectionsA.php:
namespace seculibs\collections;
class A {
Then use:
$loader->add('seculibs',__DIR__ . '/../vendor/');
And
use seculibs\collections\A;
$a = new A();