oderfla
oderfla

Reputation: 1797

Laravel routing failing for controller

Its Laravel 5. When the route.php contains this:

Route::get('/foo', function () {
    return 'Hello World';
});

then the page shows with the text "Hello World".

However, as soon as I add this new line in route.php:

Route::get('/foo2', 'IndexController');

then the page show this error:

UnexpectedValueException in Route.php line 567: Invalid route action: [App\Http\Controllers\IndexController]

I previously created a controller with artisan which now looks like this:

class IndexController extends Controller
{
    public function index()
    {
        echo 'test';
    }
}

what am I doing wrong?

Upvotes: 0

Views: 86

Answers (4)

CarlosCarucce
CarlosCarucce

Reputation: 3569

For newer versions, use

Route::get('/myroute', [MyController::class, 'methodName']); 
//or
Route::get('/myroute', ['App\\Controllers\\MyController', 'methodName']);

You have to reference Controller@method as:

Route::get('/myroute', ['uses' => 'MyController@methodName']);
//or
Route::get('/myroute', 'MyController@methodName');

Upvotes: 1

aimme
aimme

Reputation: 6763

If you are using get method of Route. Normally first argument provided should be the url and second argument should be the method (there are other ways argument could be passed)

Route::get('/foo2', 'IndexController@index');

If you want to resourceful route . Normally first argument should be the resource name and the second argument should be RESTful controller name. (there are other ways argument could be passed).Example: photo is the resource name and PhotoController is the controller name.

Route::resource('photo', 'PhotoController');

in your case it should work this way

Route::resource('/foo2', 'IndexController');

or

Route::get('/foo2', 'IndexController@index');

so when you visit

yoursite.com/foo2

you will be displayed with IndexController index method

See reference more to learn laravel's restful resource controller

reference: https://laravel.com/docs/5.1/controllers#restful-resource-controllers

Upvotes: 2

Khalid Dabjan
Khalid Dabjan

Reputation: 2797

You need to specify the function inside the controller not just the controller:

Route::get('/foo2', 'IndexController@index');

Upvotes: 1

Nico
Nico

Reputation: 7256

You have to specify wich method will be executed:

Route::get('/foo2', 'IndexController@index');

Upvotes: 2

Related Questions