Php 类存储库不存在
编辑:添加了app/route.php 我试图理解laravel 4中的测试概念。我在学习教程 在Repositories主题中,提到我们必须在app/repository文件夹中创建一个自定义接口,创建一个类(将实现该接口),然后转储自动加载composer。我们将在控制器中使用这个类 所以我创建了接口Php 类存储库不存在,php,laravel,repository,Php,Laravel,Repository,编辑:添加了app/route.php 我试图理解laravel 4中的测试概念。我在学习教程 在Repositories主题中,提到我们必须在app/repository文件夹中创建一个自定义接口,创建一个类(将实现该接口),然后转储自动加载composer。我们将在控制器中使用这个类 所以我创建了接口 <?php # app/repositories/CategoryRepositoryInterface.php interface CategoryRepositoryInterf
<?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);
}
}
但它显示了一个错误
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.json文件中,是否自动加载“存储库”目录
编辑:看起来您没有在界面中使用存储库命名空间
应该是:
# app/repositories/CategoryRepositoryInterface.php
namespace Repositories;
interface CategoryRepositoryInterface {
public function all();
public function find($id);
public function create($input);
}
我清理了名称空间,使您的文件都位于同一名称空间中,因此这不再是一个问题
app/repositories/CategoryRepositoryInterface.php
<?php namespace Repositories;
interface CategoryRepositoryInterface {
public function all();
public function find($id);
public function create($input);
}
添加了命名空间存储库,转储自动加载的编写器。还是相同的错误。你能检查一下你的composer/autoload_classmap.php吗。你的类应该在那里,名称空间应该是正确的。是的,我想它在那里。它包含一行:“Repositories\\CategoryRepositoryInterface'=>$baseDir。”/app/repositories/CategoryRepositoryInterface.php',你也能添加你的routes.php
代码吗?哈,很酷,你成功了!!!干得好,伙计!对不起,您必须将其添加到use
中,就像这样use BaseController代码>这适用于命名空间之外使用的任何类。所以你会得到更多!只需将它们添加到该列表中,如使用BaseController、Session等。我将它们添加到示例中。这是一种非常优雅的方式,可以查看您在代码中使用的类,它们都存放在一个地方。哈哈,是的,欢迎使用名称空间!;)我再看看。从哪个文件?这意味着它在名称空间之外被调用!我会找到的!:)我知道!在路由器中,必须添加命名空间的存储库\CategoryController@...'或者其他任何地方,控制器被呼叫,但我猜是路由器。不,那不好。:)控制器在某处被调用。从那个地方,它可能被称为CategoryController
,但它应该是Repositories\CategoryController
。你能在你的项目中对CategoryController进行完整的文本搜索吗?
# app/repositories/CategoryRepositoryInterface.php
namespace Repositories;
interface CategoryRepositoryInterface {
public function all();
public function find($id);
public function create($input);
}
<?php namespace Repositories;
interface CategoryRepositoryInterface {
public function all();
public function find($id);
public function create($input);
}
<?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);
}
}
<?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---------------//
}