【问题标题】:Class Repositories does not exist类存储库不存在
【发布时间】:2014-06-24 09:53:57
【问题描述】:

编辑:添加 app/route.php

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

在 Repositories 主题中,提到我们必须在 app/repository 文件夹中创建一个自定义接口,创建一个类(它将实现该接口)然后 dump-autoload 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'));
});

【问题讨论】:

  • 你能检查你的 composer/autoload_classmap.php。你的类应该在那里并且命名空间应该是正确的。
  • 是的,我想它就在那里。它包含一行:'Repositories\\CategoryRepositoryInterface' => $baseDir。 '/app/repositories/CategoryRepositoryInterface.php',
  • 您也可以添加您的routes.php 代码吗?
  • 哈,酷你搞定了!!!干得好!

标签: php laravel repository


【解决方案1】:

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

app/repositories/CategoryRepositoryInterface.php

<?php namespace Repositories;

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

app/repositories/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---------------//

}

【讨论】:

  • 抱歉,您必须将它添加到 use 中,就像 use BaseController; 这样适用于您在命名空间之外使用的任何类。所以你会得到更多!只需像use BaseController, Session, Etc 一样将它们添加到该列表中。我将它们添加到示例中。这是一种非常优雅的方式来查看您在代码中使用了哪些类,所有类都存放在一个地方。
  • 哈哈是的,欢迎来到命名空间! ;) 我再看看。来自哪个文件?
  • 这意味着它在命名空间之外被调用!我会找到的! :)
  • 我知道!在路由器中,您必须添加命名空间 'Repositories\CategoryController@...' 或任何其他调用控制器的位置,但我猜是路由器。
  • 不,那不好。 :) 控制器在某处被调用。从那个地方它可能被称为CategoryController,但这应该是Repositories\CategoryController。你能通过你的项目对CategoryController进行全文搜索吗?
【解决方案2】:

在您的 composer.json 文件中,您是否在自动加载“repositories”目录?

编辑:看起来您没有在界面中使用 Repositories 命名空间

应该是:

# app/repositories/CategoryRepositoryInterface.php

namespace Repositories;

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

【讨论】:

  • 添加了命名空间存储库,转储自动加载的作曲家。还是一样的错误。
猜你喜欢
  • 2014-02-09
  • 2014-12-31
  • 2020-12-04
  • 2021-08-01
  • 1970-01-01
  • 2014-02-01
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多