Can the Laravel ioc Container use automatic loading namespaces?

advertisements

Lets say I have a class MyClass in app/repository/MyClass.php in the Repository namespace:

namespace Repository;

class MyClass { ... }

I can bind this using the ioc container:

App::bind('SomeClass', 'Repository\MyClass');

Same thing with model dependencies:

SomeClass extends Eloquent{

    public function dependency()
    {
         return $this->hasOne('Models\Dependency');
    }        

}

One solution is to create aliases in app.php, but this has to be done for every single file that I want to have automatically namespaced:

'MyClass' => 'Repository\MyClass'

Is it possible to make the ioc container recognize the correct namespace for the classes without using aliases? Can we in any way use Composer for this?


It is not possible unless you want to register an alias for every class in your namespaces. My recent Laravel 4 project has IOC Container bindings to namespaced classes which I declared explicitly.

App::bind('Foo\Bar\Repositories\UserRepositoryInterface', 'Foo\Bar\Repositories\DbUserRepository');