Reputation: 33
I was trying to order my controllers in Laravel 4 and adding them some nice namespaces
So i wanted routes like this admin/scholar-groups And i wanted the controller to be in a subfolder called 'admin'
So I have a route file like this:
//Admin Routes
Route::group(array('namespace' => 'admin'), function()
{
Route::group(array('prefix' => 'admin'), function()
{
# Scholar Groups Controller
Route::group(['before' => 'auth|adminGroup'], function()
{
Route::resource('scholar-groups', 'ScholarGroupController');
});
});
});
Then I added a namespace to my scholarGroupController which is in a subfolder named 'admin'
<?php namespace admin;
class ScholarGroupController extends \BaseController {
/**
* Display a listing of the resource.
* GET /scholargroup
*
* @return Response
*/
public function index()
{
$scholarGroups = ScholarGroup::paginate(10);
return View::make('scholar_groups.index',compact('scholarGroups'));
}
But whenever I try to access to my index action in Controller I get this error.
Class 'admin\ScholarGroup' not found
So the namespaces is affecting my model namespace in the following line
$scholarGroups = ScholarGroup::paginate(10);
How do I avoid the namespace affecting this model class namespace?
Upvotes: 3
Views: 870
Reputation: 12157
Your controller is in the admin
namespace, and referring to other classes from there will be relative to that namespace.
So you need to refer to your model with a preceding backslash (just like you did with BaseController
) like this:
<?php namespace admin;
class ScholarGroupController extends \BaseController
{
public function index()
{
$scholarGroups = \ScholarGroup::paginate(10);
return View::make('scholar_groups.index',compact('scholarGroups'));
}
}
or import it above the class declaration like this:
<?php namespace admin;
use ScholarGroup;
class ScholarGroupController extends \BaseController
{
public function index()
{
$scholarGroups = ScholarGroup::paginate(10);
return View::make('scholar_groups.index',compact('scholarGroups'));
}
}
Also, you don't need to do Route::group
twice. you can do this:
Route::group(array('prefix' => 'admin', 'namespace' => 'admin'), function() {
// . . .
});
Upvotes: 2