2015-06-20 85 views
1

我对“高级Laravel”很新颖,但我知道大部分基础知识,并且我试图了解命名空间,接口和存储库是什么,因为我来了不是很久以前。Laravel错误界面类不存在

不过,我收到以下错误,我不知道我做错了:

类应用程序\型号\接口\ CategoriesInterface不存在

下面是我的代码:

routes.php文件

App::bind('App\Models\Interfaces\BaseInterface', 'App\Models\Repositories\BaseRepository'); 

CategoriesController.php

<?php 
use app\models\Interfaces\CategoriesInterface; 

class CategoriesController extends BaseController 
{ 
protected $categories; 

public function __construct(CategoriesInterface $categories) 
{ 
    $this->categories = $categories; 
} 

BaseInterface.php

<?php 

interface BaseInterface 
{ 
public function all(); 
} 

CategoriesInterface.php

<?php namespace App\Models\Interfaces; 
interface CategoriesInterface extends BaseInterface { } 

CategoriesRepository.php

<?php namespace app\models\Repositories; 
use App\Models\Interfaces\CategoriesInterface; 
use Categories; 

class CategoriesRepository implements CategoriesInterface 
{ 
public function all() 
{ 
    $categories = $this->categories->all(); 
    return $categories; 
} 
} 

EloquentCategoriesRepository.php

<?php namespace app\models\Repositories; 
use App\Models\Interfaces\CategoriesInterface; 
class EloquentCategoriesRepository implements CategoriesInterface { 

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

回答

0

我看到你正在尝试实施存储库模式,它起初看起来有点“高级”,但实际上很简单。

所以基本的想法是用数据库抽象你的应用程序的数据层,使你从一个DBS转换到另一个DBS(例如Mysql到Mongo)。

换句话说,您正在尝试使应用程序的业务逻辑独立于数据层(在哪里查询集合/实例),因此当您达到某个要求更改数据库的点时,您可以实现另一个存储库接口在那里提供你的应用程序和数据层之间的契约。

Laravel存储库模式的实现非常简单。

  • 创建界面
  • 创建界面的库(实际执行)
  • 绑定使用服务提供商类的库(或者你的情况App::bind
  • 实例化的依赖使用您的控制器来知识库

不要忘记使用psr-04自动加载您的名称空间。

在你的情况,我认为问题是你不自动加载名称空间。

另外CategoriesRepository.php & EloquentCategoriesRepository.php都是雄辩的存储库,将返回Eloquent集合。要返回一个stdClass(标准PDO)数组,您将不得不使用\DB外观。

如果我的回答不包括请你看一看here

+0

你是什么意思不自动加载名称空间? –

0

尝试名正常间距的类/接口。 EloquentCategoriesRepository.phpCategoriesRepository在名称空间中有app而不是App。并且CategoriesController也需要使用App\..而不是app\..

相关问题