2013-03-04 152 views
2

问题: 类PostRepostioryInterface在PostController.php 或与命名空间我甚至已经得到修补类未找到行4 应用程序\型号\接口\ PostRepositoryInterface找不到如何laravel注册一个命名空间4

问题:如何在laravel 4中注册一个名称空间?我需要做什么才能让L4在这个命名空间识别类/接口?

Larave 3曾在一个ClassLoader的命名空间$静态对象,在那里你可以通过

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

我不知道我是否有这个权利的laravel 3,但无论哪种方式,自动装载不存在添加的命名空间Laravel 4和ClassLoader存在,但Laravel 4中ClassLoader中不存在方法命名空间。

我已经看过这个,但它似乎没有注册命名空间的工作。 Using namespaces in Laravel 4

实施例结构:

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


namespaces: 
    App\Models\Repositories; 
    App\Models\Interfaces; 

文件:

PostRepositoryInterface.php

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

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

PostController.php

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

    public function __construct(PostRepositoryInterface $posts) 
    { 
     $this->posts = $posts; 
    } 

感谢

回答

1

在laravel irc频道上,我发现命名空间应该在L4中工作,而不需要在任何地方注册它们。这是因为composer dump-autoload会将它们添加到composer/autoload文件中。所以这不是问题。

这个问题很明显是一个错字(我在上面的代码中找不到它,但是在经过每一行复制/粘贴类名称和命名空间之后发生了一些变化),并且在我的真实代码中我也是如此留出了“使用”语句EloquentPostRepository.php

use App\Models\Interfaces\PostRepositoryInterface; 

现在,我已经打了另一面墙上尝试使用国际奥委会的命名空间的接口和控制器的构造函数(目标接口应用程序\型号\接口\ PostRepositoryInterface不实例化)但我想这应该是一个不同的问题。

+0

这是另一个问题,如果有人想看看它。 http://stackoverflow.com/questions/15236878/laravel-4-target-interface-is-not-instantiable – isimmons 2013-03-06 00:38:48

7

你可能忘了做composer dump-autoload。这会更新Laravel自动加载类的列表。

你可以在composer documentation了解更多。

+0

nope这将是我的猜测,但事实并非如此。我发现了一个错字和一个遗漏的使用声明,并得到了是否需要在laravel irc上注册的答案。谢谢,虽然:-) – isimmons 2013-03-05 23:52:39

相关问题