Laravel route resource with namespace
I am trying to create a resource route in Laravel for my controller which is inside app \ controllers \ FormController. How can i do this? I tried the following ways, but none of them worked.
Router::resource('form', 'app\controllers\FormController');
Router::resource('form', 'app\\controllers\\FormController');
Router::resource('form', 'app/controllers/FormController');
namespace app\controllers;
class FormController extends BaseController {
public function index()
{
return View::make('hello');
}
}
If I remove the namespace it works.
Result:
ReflectionException (-1)
Class app\controllers\FormController does not exist
source to share
You can simply do the following:
Router::resource('form', 'FormController');
All classes in are app/controllers/
automatically loaded by Laravel.
Update:
You need to change the index function to getIndex()
. If you are using resource routing, each function must start with a request method.
source to share
app/controllers
loaded by default. but if you are using a different namespace you can use that.
eg. namespace Site
;
Route::resource('form', '\Site\FormController');
there is another way.
let's say there are different controllers in the same namespace. for example FormController
, 'BlogController`. you can group it.
Route::group(['namespace' => 'Site'], function()
{
Route::resource('form', 'FormController');
Route::resource('blog', 'BlogController');
});
update # 1:
Route::resource('form', 'FormController');
you don't need to use any namespace.
source to share