类存储库不存在


Class Repositories does not exist

EDIT: add app/route.php

我试图理解在laravel 4测试的概念。我遵循这个教程。

在repository主题中提到,我们必须在app/repository文件夹中创建一个自定义接口,创建一个类(它将实现该接口),然后转储自动加载编写器。我们将在控制器中使用这个类。

所以我创建了接口
<?php
# app/repositories/CategoryRepositoryInterface.php
interface CategoryRepositoryInterface {
    public function all();
    public function find($id);
    public function create($input);  
}

然后创建实现这个接口的类

<?php
# app/repositories/EloquentCategoryRepository.php
namespace Repositories
use Repositories'CategoryRepositoryInterface;
use Category;
class EloquentCategoryRepository implements CategoryRepositoryInterface {
    public function __construct(Category $category){
        $this->category = $category;
    }
    public function all()
    {
        return $category->all();
    }
    public function find($id)
    {
        return $category->find($id);
    }
    public function create($input)
    {
        return $category->create($input);
    }
}

dump自动加载它,然后尝试在我的控制器中使用它

<?php
# app/controllers/CategoryController.php
use Repositories'CategoryRepositoryInterface as Category;
class CategoryController extends BaseController {
    public function __construct(Category $category){
        $this->category = $category;
    }
    public function getCategory(){  
        $categories = $this->category->all();
        return View::make('dashboard.showcategories')->with('categories', $categories);
    }
    public function editCategory($id){
        $category = $this->category->find($id);
        return View::make('dashboard.editcategory')->with('category', $category);
    }
    public function updatecategory(){
        //print_r(Input::all());
        $category = $this->category->find(Input::get('id'));
        $category->category_name = Input::get('category_name');
        $category->options = Input::get('options');
        $category->save();
        Session::flash('message', 'Category Updated Successfully');
        return Redirect::route('categories');
        //<center><a href="{{url('category')}}">(Back)</a></center>
    }
    public function deleteCategory($id){
        $category = $this->category->find($id)->delete();
        Session::flash('message', 'Category Deleted Successfully');
        return Redirect::route('categories');
    }
//----------Showing and progressing New category form---------------//
    public function getCategoryForm(){
        return View::make('dashboard.addcategoryform');
    }
    public function postCategoryForm(){
        if(Input::get('category_name')!="" && Input::get('options')!==""){
                $category = new Category;
                $category->category_name = Input::get('category_name');
                $category->options = Input::get('options');
                $category->save();
                Session::flash('message', 'Category Added Successfully');
                return Redirect::route('categories');
        }else return Redirect::to('addcategory');
    }
//----------(End) Showing and progressing category form---------------//
}

然后在路由文件

中使用App::bind()
App::bind(
                'Repositories'CategoryRepositoryInterface',
                'Repositories'EloquentCategoryRepository'
            );

但是显示错误

ReflectionException
Class Repositories'CategoryRepositoryInterface does not exist

我做错了什么?

这是路由文件:

<?php

Route::get('/', array('as'=>'getlogin', 'uses'=>'dashboardController@getLogin'));
Route::get('login', array('as'=>'getlogin', 'uses'=>'dashboardController@getLogin'));
Route::post('login', array('as'=>'postlogin', 'uses'=>'dashboardController@postLogin'));

Route::get('test', array('as'=>'test', 'uses'=>'dashboardController@test'));
Route::post('test', array('as'=>'testpost', 'uses'=>'dashboardController@postTest'));
Route::group(array('before'=>'auth'), function(){
    Route::get('dashboard', array('as'=>'dashboard', 'uses'=>'dashboardController@dashboard'));
    Route::get('users', array('as'=>'users', 'uses'=>'dashboardController@users'));
        Route::get('users/delete/{userid?}',array('as'=>'deleteuser', 'uses'=>'dashboardController@delete'));
        Route::get('users/contacts/{userid?}', array('as'=>'contacts', 'uses'=>'dashboardController@contacts'));
    Route::get('geo', array('as'=>'geo', 'uses'=>'dashboardController@geo'));

    //---------------------------------Categories Routes--------------------------//
    Route::get('categories', array('as'=>'categories', 'uses'=>'Repositories'CategoryController@getCategory'));
        Route::group(array('prefix'=>'categories'), function(){
            App::bind(
                'Repositories'CategoryRepositoryInterface',
                'Repositories'EloquentCategoryRepository'
            );
            Route::get('addcategory',array('as'=>'getcategoryform', 'uses'=>'Repositories'CategoryController@getCategoryForm'));
            Route::post('addcategory', array('as'=>'postcategoryform', 'uses' => 'Repositories'CategoryController@postCategoryForm'));
            Route::get('edit/{id}', array('as'=>'editcategory', 'uses'=>'Repositories'CategoryController@editCategory'));
            Route::post('updatecategory', array('as'=>'updatecategory', 'uses'=>'Repositories'CategoryController@updatecategory'));
            Route::get('deletecategory/{id}', array('as'=>'deletecategory', 'uses'=>'Repositories'CategoryController@deleteCategory'));
        });
    //----------------------------------End categories-----------------------------//
    Route::get('dashboard/hoots', array('as'=>'hoots', 'uses'=>'dashboardController@hoots'));
    Route::get('dashboard/uniqueusers', array('as'=>'uniqueusers', 'uses'=>'dashboardController@uniqueUsers'));

    Route::get('logout', array('as'=>'logout', 'uses'=>'dashboardController@logout'));
});

我清理了名称空间,所以你的文件都在同一个名称空间中,所以这不再是一个问题了。

app/仓库/CategoryRepositoryInterface.php

<?php namespace Repositories;
interface CategoryRepositoryInterface {
    public function all();
    public function find($id);
    public function create($input);  
}

app/仓库/EloquentCategoryRepository.php

<?php namespace Repositories;
use Category;
class EloquentCategoryRepository implements CategoryRepositoryInterface {
    public function __construct(Category $category){
        $this->category = $category;
    }
    public function all()
    {
        return $category->all();
    }
    public function find($id)
    {
        return $category->find($id);
    }
    public function create($input)
    {
        return $category->create($input);
    }
}

app/controllers/CategoryController.php

<?php namespace Repositories;
use CategoryRepositoryInterface as Category;
use BaseController, View, Input, Session, Redirect;
class CategoryController extends BaseController {
    public function __construct(Category $category){
        $this->category = $category;
    }
    public function getCategory(){  
        $categories = $this->category->all();
        return View::make('dashboard.showcategories')->with('categories', $categories);
    }
    public function editCategory($id){
        $category = $this->category->find($id);
        return View::make('dashboard.editcategory')->with('category', $category);
    }
    public function updatecategory(){
        //print_r(Input::all());
        $category = $this->category->find(Input::get('id'));
        $category->category_name = Input::get('category_name');
        $category->options = Input::get('options');
        $category->save();
        Session::flash('message', 'Category Updated Successfully');
        return Redirect::route('categories');
        //<center><a href="{{url('category')}}">(Back)</a></center>
    }
    public function deleteCategory($id){
        $category = $this->category->find($id)->delete();
        Session::flash('message', 'Category Deleted Successfully');
        return Redirect::route('categories');
    }
//----------Showing and progressing New category form---------------//
    public function getCategoryForm(){
        return View::make('dashboard.addcategoryform');
    }
    public function postCategoryForm(){
        if(Input::get('category_name')!="" && Input::get('options')!==""){
                $category = new Category;
                $category->category_name = Input::get('category_name');
                $category->options = Input::get('options');
                $category->save();
                Session::flash('message', 'Category Added Successfully');
                return Redirect::route('categories');
        }else return Redirect::to('addcategory');
    }
//----------(End) Showing and progressing category form---------------//
}

在您的编写器中。Json文件,你是自动加载"仓库"目录吗?

编辑:看起来你没有在你的接口中使用repository命名空间

应:

# app/repositories/CategoryRepositoryInterface.php
namespace Repositories;
interface CategoryRepositoryInterface {
    public function all();
    public function find($id);
    public function create($input);  
}