Silex $app['autoloader']->registerNamespace() deprecated

The past few days I’ve been playing around with Silex, a micro PHP Framework. At a certain point I got stuck in the process when using a custom controller: the darn class just wouldn’t load and the (otherwise excellent) documentation on the Silex site has not mention on how to load it.

Most of the information one finds on the internet instruct you to do this (line #3):

// source for /app/bootstrap.php
require_once __DIR__ . '/../vendor/autoload.php';
$app = new Silex\Application();
$app['autoloader']->registerNamespace('Bramus', __DIR__. '/src');

That information however, is deprecated and won’t work.

A working solution I eventually found was this one:

// source for /app/bootstrap.php
$loader = require_once __DIR__ . '/../vendor/autoload.php';
$app = new Silex\Application();
$loader->add('Bramus', __DIR__. '/../src');

But that code just stinks I must say, it just doesn’t feel right.

Turns out the nicest solution is the most simple one: just register your custom namespace in composer.json. For example:

	"require": {
		"silex/silex": "1.0.*@dev"
	"autoload": {
		"psr-0": {
			"Bramus": "src/"

After changing it, run a composer update and you’re good to go.

Hope this helped you, struggled with it myself quite some time.

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

One reply on “Silex $app['autoloader']->registerNamespace() deprecated”

  1. How would I use the recently loaded class as a controller function?

    The silex docs states tha one could ‘Blah::get’ but I always get Class “Blah” does not exist.

Comments are closed.