Getting GET “?” variable in laravel

前端 未结 8 820
夕颜
夕颜 2020-12-16 08:41

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

Everything works well as expected.

Now, I want to map GET request to recognise variabl

8条回答
  •  星月不相逢
    2020-12-16 09:31

    It is not very nice to use native php resources like $_GET as Laravel gives us easy ways to get the variables. As a matter of standard, whenever possible use the resources of the laravel itself instead of pure PHP.

    There is at least two modes to get variables by GET in Laravel ( Laravel 5.x or greater):

    Mode 1

    Route:

    Route::get('computers={id}', 'ComputersController@index');
    

    Request (POSTMAN or client...):

    http://localhost/api/computers=500
    

    Controler - You can access the {id} paramter in the Controlller by:

    public function index(Request $request, $id){
       return $id;
    }
    

    Mode 2

    Route:

    Route::get('computers', 'ComputersController@index');
    

    Request (POSTMAN or client...):

    http://localhost/api/computers?id=500
    

    Controler - You can access the ?id paramter in the Controlller by:

    public function index(Request $request){
       return $request->input('id');
    }
    

提交回复
热议问题