Galivan
Galivan

Reputation: 5328

Laravel route with optional parameter to controller

A route with optional parameter works when doing like this:

Route::get('{anything}',function($anything){echo $anything;});    

but I would like to use a controller. Doing like this produces an error:

Route::get('{anything}','redirectController');

controller:

class redirectController {

public function index($anything){
    echo $anything;
}} 

What may be the problem? (using laravel 4.2)

update: I renamed the controller with capital letter, and tried this:

Route::get('{anything}',['uses' => 'RedirectController@index']); 

but it's still an error: "Call to undefined method RedirectController::getAfterFilters() ".

Upvotes: 0

Views: 1333

Answers (1)

Armen Markossyan
Armen Markossyan

Reputation: 1214

If you want to use a controller there're two options:

  1. Route::controller('route', 'SomeController');
  2. Route::get('route', ['uses' => 'SomeController@index']);;

In the first case you have to read this:
http://laravel.com/docs/4.2/controllers#implicit-controllers

Name of your action in this case should be getIndex, not just index.

Good luck!

UPD

Make sure your controller extends Laravel's Controller class like follows:

use Illuminate\Routing\Controller;

class SomeController extends Controller {
    ...
}

Upvotes: 2

Related Questions