问题:
PostController.php 中第 4 行未找到类 PostRepostioryInterface
或者在修改命名空间时我什至得到了类
找不到应用\模型\接口\PostRepositoryInterface
问题:如何在 laravel 4 中注册命名空间?我需要做什么才能让 L4 识别此命名空间中的类/接口?
Laravel 3 在 ClassLoader 中有一个 $namespaces 静态对象,您可以通过以下方式添加命名空间
Autoloader::namespaces(array(
'App\Models\Interfaces' => path('app').'models/interfaces',
));
我不确定我是否有 Laravel 3 的权利,但无论如何,Laravel 4 中不存在 AutoLoader,而 Laravel 4 中存在 ClassLoader,但方法名称空间不存在于 Laravel 4 中的 ClassLoader 中。
我已经看过这个,但如果不以某种方式注册命名空间,它似乎不起作用。在 Laravel 4 中使用命名空间 https://stackoverflow.com/questions/14714848/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;
}
Thanks