0

This post is dedicated to the easy solution that seems to exist to add your own namespaces, the solution with the loader in app/autoload.php.

There is a lot of documentations talking about the magic methods like registerNamespace or registerPrefix.

The problem is that those methods exist for a UniversalClassLoader object.

I downloaded the Symfony standard edition 2.2, and the app/autoload.php looks more like that (pretty much the same with Symfony standard edition 2.1) :

use Doctrine\Common\Annotations\AnnotationRegistry;

$loader = require __DIR__.'/../vendor/autoload.php';

// intl
if (!function_exists('intl_get_error_code')) {
  require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';
}

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

return $loader;

the loader used in fact is the composer loader. The only method you could use is the 'add' method like this if you hope to add 'seculibs/collections' namespace for example:

$loader->add("seculibs\\collections", __DIR__.'/../vendor/seculibs/collections/');

But it does not seem to work : when I execute programm I have the same classNotFound for /seculibs/collections/xx.php

So I changed the autoload.php like that :

    require_once ('/../vendor/symfony/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php');

use Doctrine\Common\Annotations\AnnotationRegistry;
use Symfony\Component\ClassLoader\UniversalClassLoader;

$loader = require __DIR__.'/../vendor/autoload.php';
$universalLoader = new UniversalClassLoader();

$universalLoader->registerNamespace("seculibs\\collections", __DIR__.'/../vendor/seculibs/collections/');
$universalLoader->register();

// intl
if (!function_exists('intl_get_error_code')) {    
    require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';

}

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

return $loader;

Nothing...

But obviously it works for a lot of persons so.. what am I doing wrong ? Do they have some other Symfony version that would be found on secret websites ?

one of the classes is like that :

namespace seculibs\collections;

class LinkedMap {
    private $items;

    public function __construct() {
        $this->items = array();
    }

    public function __destruct() {
        unset($this->items);
    }

 ....
4
  • What are you trying to achieve? Commented Mar 7, 2013 at 15:20
  • I have 2 classes in vendor/seculibs/collections...and I want to use them in my bundles using the loader method Commented Mar 7, 2013 at 15:37
  • And are these classes themselves namespaced? Post the first few lines (up to and including the class line) from one of them and someone can probably give you an exact answer. Commented Mar 7, 2013 at 16:21
  • ok course... I update my answer to show you one of the classes Commented Mar 7, 2013 at 16:23

2 Answers 2

1
$loader->add('seculibs\\collections',__DIR__ . '/../vendor');

new LinkedMap();

Assuming you have file: vendor/seculibs/collections/LinkedMap.php

Normally, you would have another level in your library. Something like:

vendor/MyStuff/seculibs/collections

And then the add line would point to vendor/MyStuff

Sign up to request clarification or add additional context in comments.

8 Comments

why just vendor ? why not vendor/seculibs/collections ? my namespace is for this location not all vendor no ?
Because that is the way it works. You point to the directory containing your namespace and not the classes. That is also why I said that you would normally have another directory in there. Look at the rest of the vendors and see how they do it.
ok... it works... but I am still wating for someone to make it works with the methods 'registerNamespace' and so on... if nobody answers for that... I will give you the point because your help was much appreciated
S2.0 used it's own UniversalLoaderClass which had an registerNamespace method. S2.1 switched to using the composer loader which only has an add method. So basically, you need to make sure you are looking at the correct documentation.
interesting... but I am looking in the write documentation : The class loader component ... they even say inside the documentation that in Symfony 2.1 they added a new method on the loader object they are talking about
|
1

You can add your own libraries to the composer.json autoload config, so even though they aren't loaded by composer, they will be in the generated autloader.

"autoload": {
    "psr-0": {
        "": "src/",
        "MyLib_": "/home/sites/MyLib"
    }
},

11 Comments

I tried that, it does work for me, I dont know why... plus I want to use the method with the loader... thank you
OK, that style does work, so it's something else. Why don't you want to let composer handle it?
because I added : "seculibs\\collections": "vendor/seculibs/collections" in the composer.json and it did not change anything... plus I want to understand why the documentation gives one solution and it cannot be applied as I see it now
By the way, I guess that after I have changed the composer.json, I have to write a command line with composer.phar right?
Check what it puts into the generate autoloader (vendor/composer/autoload_namespaces.php) Mine maps correctly using that format.
|

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.