2

In order to add parameter to a GET method, I know that I have to add {parameter} in the route like the following

Route::get('example/search/{id}', 'ExampleController@exampleMethod')

However, is there a way to do this using the RESTful controller like the following?

routes.php

Route::controller('example', 'ExampleController')

ExampleController.php

public function getSearch($id){
    //do something with $id
}

The above doesn't work because the routes.php doesn't expect a parameter to getSearch method. I wonder if there is a way to solve this without having to add individual Route::get routes.

1 Answer 1

3
<?php

// ExampleController.php

class ExampleController extends BaseController {
    public function getSearch($id = null){
        if ($id == null) {
            return 'no id';
        }
        return $id;
    }
}

// routes.php

Route::controller('example', 'ExampleController');

?>

php artisan routes:

enter image description here

enter image description here

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

2 Comments

Great. But what about same situation with the "index" method in Laravel 4? Even if you define it as public function getIndex($id = null), requesting URL domain.tld/example/1 throws Controller method not found error. You have to define a seperate route for that, like Route::get('example/{id}', ['uses' => 'ExampleController@getIndex'])->where('id', '[0-9]+');. NOTE: this route decralation should take place BEFORE your Route::controller declaration to work.
@vitalikaz Index has a very specific use. It is supposed to be the index page in your application. Therefore it is not supposed to take parameters by design as far as I understand so what you are doing is overriding the desired default behavior.

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.