我是Laravel的新手并且通常使用PHP命名空间.在我决定制作名为File的模型之前,我没有遇到任何问题.我如何正确地进行命名空间,以便我可以使用我的File模型类?
文件是app/controllers/FilesController.php和app/models/File.php.我正在努力创造一个新File的FilesController.php.
这与此问题有关如何在laravel 4中注册命名空间,但我相信我已经解决了这个问题,而命名空间现在正在运行.
我遇到了一个新问题.我相信错误来自于尝试在控制器构造函数中键入提示,并且与使用命名空间和使用ioc有关.
BindingResolutionException: Target [App\Models\Interfaces\PostRepositoryInterface] is not instantiable.
Run Code Online (Sandbox Code Playgroud)
下面的方法工作得很好,直到我尝试引入命名空间.我可以删除所有名称空间并将接口和存储库放在同一目录中,但是想知道如何使用这种使用ioc的方法来使用名称空间.
这是相关文件.
routes.php文件
Route::resource('posts', 'PostsController');
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)
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();
//after above edit it works to this point
//error: …Run Code Online (Sandbox Code Playgroud)