【发布时间】: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