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

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

php - Getting GET "?" Variable in Laravel

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

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

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...