Monthly Archives: April 2013

How To: Fix Silex & Composer Autoloading – Class Not Found Error

I started a new project in Silex this weekend and spent a while trying to figure this one out.

Fatal error: Class 'App\Model\MyAmazingClass' not found in /var/www/app/public/index.php on line 18

So, silex depends on Composer for its autoloading functionality. I normally handle my dependencies myself and build stuff from source but this is a quick project, let’s give composer a go. So, off to the composer blog to get my composer.json set up. It tells me to add the following section:

    "autoload": {
        "psr-0": {
            "App" : "src"
        }
    }

Okay, now a “composer update” to register the new dependency with composer and I can see that this line has been added to AutoLoad_Namespaces.php:

    'App' => $baseDir . '/app',

Long story short, I was expecting this line to have mapped my ‘App’ namespace to the ‘/app’ folder and expecting that a model folder under /app with my class in it would be autoloaded (crazy right?). What composer needs you to do is create your ‘App’ namespace folder under the directory that you’ve already defined for it.

Anyway, hope this helps someone else out there.