justmyfreak justmyfreak - 2 months ago 9
PHP Question

Getting GET "?" variable in laravel

Hello I'm creating API using REST and Laravel, I was following this article. Everything went well, but I want to map GET request to recognise variable using "?". For example:

domain/api/v1/todos?start=1&limit=2


My routes.php :

Route::any('api/v1/todos/(:num?)', array('as' => 'api.todos', 'uses' => 'api.todos@index'));


my controllers/api/todos.php :

class Api_Todos_Controller extends Base_Controller {

public $restful = true;

public function get_index($id = null) {
if(is_null($id)) {
return Response::eloquent(Todo::all(1));

} else {
$todo = Todo::find($id);
if (is_null($todo)) {
return Response::json('Todo not found', 404);
} else {
return Response::eloquent($todo);
}
}
}
}


How to recognise get parameter using "?" ?
Thank you and sorry for my bad English

Answer

Take a look at the $_GET and $_REQUEST superglobals. Something like the following would work for your example:

$start = $_GET['start'];
$limit = $_GET['limit'];

EDIT

According to this post in the laravel forums, you need to use Input::get(), e.g.,

$start = Input::get('start');
$limit = Input::get('limit');

See also: http://laravel.com/docs/input#input

Comments