tzortzik
tzortzik

Reputation: 5133

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 in 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

Upvotes: 3

Views: 5955

Answers (2)

itachi
itachi

Reputation: 6393

app/controllers are loaded by default. but if you are using different namespace, you can use that.

e.g. namespace is Site;

Route::resource('form', '\Site\FormController');

there is another way.

let's say there are different controllers in the same namespace. e.g. 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.

Upvotes: 6

Jerodev
Jerodev

Reputation: 33186

You can just do the following:

Router::resource('form', 'FormController');

All classes in app/controllers/ are automaticly loaded by Laravel.

Update: You need to change the index function to getIndex(). If you use resource routing, every function has to start with the request method.

Upvotes: 5

Related Questions