EDIT: added app/route.php
i am trying to understand the concept of testing in laravel 4. i was following this tutorial.
In the Repositories topic, it is mentioned that we must create a custom interface in the app/repository folder, create a class (which will implement the interface) and then dump-autoload the composer. We will use this class in our controller.
So i created the interface
<?php
# app/repositories/CategoryRepositoryInterface.php
interface CategoryRepositoryInterface {
public function all();
public function find($id);
public function create($input);
}
then created the class which implements this interface
<?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 autoloaded it, then tried to use it in my controller
<?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---------------//
}
Then used App::bind() in the route file
App::bind(
'Repositories\CategoryRepositoryInterface',
'Repositories\EloquentCategoryRepository'
);
But it's showing an error
ReflectionException
Class Repositories\CategoryRepositoryInterface does not exist
What am i doing wrong?
here's the route file:
<?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'));
});
I cleaned up the namespaces so your files are all in the same namespace, so this is not a problem anymore.
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---------------//
}
In your composer.json file, are you autoloading the "repositories" directory?
EDIT: Looks like you aren't using the Repositories namespace in your Interface
Should be:
# app/repositories/CategoryRepositoryInterface.php
namespace Repositories;
interface CategoryRepositoryInterface {
public function all();
public function find($id);
public function create($input);
}