Search code examples
phpsymfonyautoloadersilex

What is wrong with this use of Silex' autoloader?


My application file:

<?php // /src/app.php

require_once __DIR__ . '/../lib/vendor/Sensio/silex.phar';

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Foo\Bar;

$app = new Silex\Application(); 
$app['autoloader']->registerNamespace('Foo', __DIR__);
$bar = new Bar();
(...)

My Bar class:

<?php /src/Bar.php

namespace Foo;

use Silex\Application;
use Silex\ControllerProviderInterface;
use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Response;

class Bar implements ControllerProviderInterface { ... }

When I do a $bar = new Bar() in my app.php, I get an error: Fatal error: Class 'Moken\Classname' not found in (...)/src/app.php on line 11

Can anyone tell me what I am doing wrong?


Solution

  • If you use namespace Foo; you must locate this class in Foo directory
    Every namespace part is a directory in symfony

    If not works, you must show the loader where to find this class In symfony2 I use for this:

    use Symfony\Component\ClassLoader\UniversalClassLoader;
    
    $loader = new UniversalClassLoader();
    $loader->registerNamespaces(array(
        // HERE LOCATED FRAMEWORK SPECIFIED PATHS
    
        // app namespaces
        'Foo' => __DIR__ . '/../src',
    ));