类存储库不存在

时间:2014-05-07 12:13:07

标签: php laravel repository

编辑:添加了app / route.php

我试图理解laravel 4中的测试概念。我正在关注this教程。

在Repositories主题中,提到我们必须在app / repository文件夹中创建一个自定义接口,创建一个类(将实现该接口),然后dump-autoload the composer。我们将在控制器中使用此类。

所以我创建了界面

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

2 个答案:

答案 0 :(得分:1)

在您的composer.json文件中,您是否正在自动加载&#34;存储库&#34; ?目录

编辑:您似乎未在界面

中使用Repositories命名空间

应该是:

# app/repositories/CategoryRepositoryInterface.php

namespace Repositories;

interface CategoryRepositoryInterface {
    public function all();
    public function find($id);
    public function create($input);  
}

答案 1 :(得分:1)

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

应用程序/资源库/ CategoryRepositoryInterface.php

<?php namespace Repositories;

interface CategoryRepositoryInterface {
    public function all();
    public function find($id);
    public function create($input);  
}

应用程序/资源库/ 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);
    }
}

应用程序/控制器/ 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---------------//

}
相关问题