Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hello I'm creating an API using REST and Laravel following this article.

Everything works well as expected.

Now, I want to map a GET request to recognise a variable using "?".

For example: domain/api/v1/todos?start=1&limit=2.

Below is the contents of 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 do I GET a parameter using "?" ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
303 views
Welcome To Ask or Share your Answers For Others

1 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...