3

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

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried both ways and none of them worked. I updated my first post
5

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.

2 Comments

I updated my first post with the error. I used this resource: scotch.io/tutorials/…
It worked. I had the BaseController under a different namespace

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.