The following is a portion of my routing file. However, whenever I visit the /recipe
or /recipe/{id}
url's i get the error
Cannot redeclare class Recipe.
Recipe is the name of my model btw.
Route::group(array('before' => 'auth'), function(){
Route::any('/logout', 'LoginController@actionLogout');
Route::get('/recipe/add', 'RecipeController@showAdd');
Route::post('/recipe/add', 'RecipeController@actionAdd');
});
Route::any('/', 'HomeController@actionHome');
Route::get('/recipe', 'RecipeController@showIndex');
Route::get('/recipe/{id}', 'RecipeController@showItem');
If I alter the routing file to below the error no longer occur's. However, the url's are then obviously affected by the 'before'=>'auth' meaning it can only be accessed by those which are logged in.
Route::group(array('before' => 'auth'), function(){
Route::any('/logout', 'LoginController@actionLogout');
Route::get('/recipe/add', 'RecipeController@showAdd');
Route::post('/recipe/add', 'RecipeController@actionAdd');
Route::any('/', 'HomeController@actionHome');
Route::get('/recipe', 'RecipeController@showIndex');
Route::get('/recipe/{id}', 'RecipeController@showItem');
});
Am I using groups wrong, or am I missing something.
Edit: The controller file is as follows:
<?
class RecipeController extends BaseController
{
protected $layout = 'layouts.home';
public function showIndex()
{
$this->layout->content = View::make('recipe.index')->with('recipes', Recipe::paginate(15));
}
public function showAdd()
{
$this->layout->content = View::make('recipe.add');
}
public function actionAdd()
{
$recipe = new Recipe;
$recipe->creator = Auth::user()->id;
$recipe->title = Input::get('name');
$recipe->serves = Input::get('serves');
if (Input::hasFile('image')){
$fileName = date('m_d_Y_h_i_s_a', time()).'__'.str_replace(' ', '', Input::file('image')->getClientOriginalName());
Input::file('image')->move('uploads', $fileName);
$recipe->image = $fileName;
}
$recipe->save();
Log::info(Input::get('method'));
foreach (Input::get('method') as $index => $item){
$method = new MethodItem();
$method->content = $item;
$method->order = $index;
$recipe->Method()->save($method);
}
return Redirect::to('/recipe')->with('message','Wrong Username or Password');
}
public function showItem($id)
{
$recipe = Recipe::findOrFail($id);
$this->layout->content = View::make('recipe.item')->with('recipe', $recipe);
}
}
And the model:
<?php
class Recipe extends Eloquent{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'recipes';
public function Creator()
{
return $this->belongsTo('User', 'creator');
}
public function Method(){
return $this->hasmany('MethodItem', 'recipe')->orderBy('order');
}
}
Edit 2
It appears to be a problem with how Laravel's (or composers) autoloader is working. It appears that at the controller run-time the recipe model has not been instantiated, as detected with class_exists('Recipe', false)
. However, when i run class_exists('Recipe', true)
the error occurs.
Edit3
So the sort or answer I have found is to rename the recipe class (and file name, plus all references to it). I then had to create a blank file named recipe.php. This feels incredibly hacky, but it works so im going to stick with it for now. I wonder if maybe the controllers need to have completely separate name's from the Eloquent models?
The solution is to re-generate the autoload files. For me i just ran composer dump-autoload -o
. It appears some users may need composer dump-autoload
.
Many thanks to user2094178 for providing the solution.
The answer is simple. You already declared a Recipe
class. It has nothing to do with your routes file; so stop looking there. Do a search on your entire project with the query "class Recipe".