Question

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.

Was it helpful?

Solution

<?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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top