How do you register a namespace with Silex autoloader How do you register a namespace with Silex autoloader symfony symfony

How do you register a namespace with Silex autoloader


In recent versions of Silex the autoloader is deprecated and you should register all your namespaces through the composer.json file which imo is a nicer solution because you are centralizing your autoloading definitions.

Example:

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

In fact when you try to access the autoloader in any recent version of Silex the following RuntimeException is thrown:

You tried to access the autoloader service. The autoloader has been removed from Silex. It is recommended that you use Composer to manage your dependencies and handle your autoloading. See http://getcomposer.org for more information.


I'd use

$app['autoloader']->registerNamespace('MyNamespace', __DIR__.'/../lib');


Deprecated - As of 2014-10-21 PSR-0 has been marked as deprecated.PSR-4 is now recommended as an alternative

That is why you should use PSR-4 syntax in composer.json

{  "require": {      "silex/silex": "1.0.*@dev",  },  "autoload": {      "psr-4": {          "Vendor\\Namespace\\": "/path"      }  }}