如何在laravel 4中注册命名空间

isi*_*ons 2 laravel laravel-4

问题:在PostController.php的第4行找不到类PostRepostioryInterface或者修改命名空间我甚至没有找到类App\Models\Interfaces\PostRepositoryInterface

问题:如何在laravel 4中注册命名空间?要让L4识别此命名空间中的类/接口,我需要做什么?

Larave 3在ClassLoader中有一个$ namespaces静态对象,您可以在其中添加名称空间

Autoloader::namespaces(array(
  'App\Models\Interfaces' => path('app').'models/interfaces',
));
Run Code Online (Sandbox Code Playgroud)

我不确定我是否拥有laravel 3的权利,但无论如何,Laravel 4中不存在AutoLoader,并且ClassLoader存在,但Laravel 4中的ClassLoader中不存在方法命名空间.

我已经看过这个但是如果没有以某种方式注册命名空间它似乎没有用. 在Laravel 4中使用名称空间

结构示例:

app/models/interfaces
    PostRepostitoryInterface.php
app/models/repositories
    EloquentPostRepository.php


namespaces:
    App\Models\Repositories;
    App\Models\Interfaces;
Run Code Online (Sandbox Code Playgroud)

文件:

PostRepositoryInterface.php

<?php namespace App\Models\Interfaces;
interface PostRepositoryInterface {
    public function all();
    public function find($id);
    public function store($data);
}
Run Code Online (Sandbox Code Playgroud)

EloquentPostRepository.php

<?php namespace App\Models\Repositories;
use App\Models\Interfaces\PostRepositoryInterface;
class EloquentPostRepository implements PostRepositoryInterface {

    public function all()
    {
        return Post::all();
    }

    public function find($id)
    {
        return Post::find($id);
    }

    public function store($data)
    {
        return Post::save($data);
    }
}
Run Code Online (Sandbox Code Playgroud)

PostController.php

<?php
use App\Models\Interfaces\PostRepositoryInterface;
class PostsController extends BaseController {

    public function __construct( PostRepositoryInterface $posts )
    {
        $this->posts = $posts; 
    }
Run Code Online (Sandbox Code Playgroud)

谢谢

rmo*_*bis 7

你可能忘记了composer dump-autoload.这将更新Laravel自动加载类的列表.

您可以阅读有关作曲家文档的更多信息.