質問

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');

他のヒント

You could use

cd /path/to/laravel/
php composer dump-autoload

to have laravel see all the (new) classes and load them correctly

Add app/repository to the autoload section of the composer.json file, like this for example:

"autoload": {
    "classmap": [
         "app/commands",
         "app/controllers",
         "app/models",
         "app/database/migrations",
         "app/database/seeds",
         "app/tests/TestCase.php",
         "app/repository"
    ]
},

Then run composer dump-autoload.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top