0

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 Answer 1

2

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 {
    ...
}
Sign up to request clarification or add additional context in comments.

5 Comments

So the controller function should be 'getIndex'?. I tried Route::get('{anything}',['uses' => 'RedirectController@index']); but it didn't work..
'RedirectController@index' means that it will use RedirectController and call index (not getIndex in this case) action. Make sure that your class name is RedirectController but not redirectController because class names by convention should start with a capital letter.
Okay, I renamed it. using just 'index' , it still doesn't work for me
See the UPD part of the answer. I highly recommend you to read documentation.
Thanks a lot! " extends BaseController" solved it. I'm very new to laravel, I have to admit.

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.