Laravel route with optional parameter to controller

1.3k views Asked by At

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() ".

1

There are 1 answers

5
Armen Markossyan On BEST ANSWER

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 {
    ...
}