1

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

1
  • 1
    You shouldn't be putting things in the vendor folder. The stuff in there is purely for write access. With regards to the lack of loading, as annoying as it is, sometimes (providing everything is correct but it's just not "clicking") a reboot does the trick Commented Dec 16, 2013 at 1:20

3 Answers 3

1

So I found the right syntax... It will help some others

$loader->add('seculibs\\collections',__DIR__ . '/../vendor');
Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

I understand that... I am talking if I want to add them in the vendor folder
see my update, the file location does not matter. What matters is the namespace
My classes are namespaced... it does not change anything, I have to load them... namespace is not enough
0

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

2 Comments

My classes are namespaced so I wrote : $loader->add('seculibs',DIR . '/../vendor/seculibs/collections'); and when I call them I use the seculibs/collections/name_of_my_class but it does not work
Update your question and include the top part of one of your classes (i..e. namespace and then class deceleration statement. And then show a sample new statement. I also updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.