Getting GET “?” variable in laravel

前端 未结 8 805
夕颜
夕颜 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:25

    Query params are used like this:

    use Illuminate\Http\Request;
    
    class MyController extends BaseController{
    
        public function index(Request $request){
             $param = $request->query('param');
        }
    
    0 讨论(0)
  • 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');
    }
    
    0 讨论(0)
  • 2020-12-16 09:35

    I haven't tested on other Laravel versions but on 5.3-5.8 you reference the query parameter as if it were a member of the Request class.

    1. Url

    http://example.com/path?page=2

    2. In a route callback or controller action using magic method Request::__get()

    Route::get('/path', function(Request $request){
     dd($request->page);
    }); 
    
    //or in your controller
    public function foo(Request $request){
     dd($request->page);
    }
    
    //NOTE: If you are wondering where the request instance is coming from, Laravel automatically injects the request instance from the IOC container
    //output
    "2"
    

    3. Default values

    We can also pass in a default value which is returned if a parameter doesn't exist. It's much cleaner than a ternary expression that you'd normally use with the request globals

       //wrong way to do it in Laravel
       $page = isset($_POST['page']) ? $_POST['page'] : 1; 
    
       //do this instead
       $request->get('page', 1);
    
       //returns page 1 if there is no page
       //NOTE: This behaves like $_REQUEST array. It looks in both the
       //request body and the query string
       $request->input('page', 1);
    

    4. Using request function

    $page = request('page', 1);
    //returns page 1 if there is no page parameter in the query  string
    //it is the equivalent of
    $page = 1; 
    if(!empty($_GET['page'])
       $page = $_GET['page'];
    

    The default parameter is optional therefore one can omit it

    5. Using Request::query()

    While the input method retrieves values from entire request payload (including the query string), the query method will only retrieve values from the query string

    //this is the equivalent of retrieving the parameter
    //from the $_GET global array
    $page = $request->query('page');
    
    //with a default
    $page = $request->query('page', 1);
    

    6. Using the Request facade

    $page = Request::get('page');
    
    //with a default value
    $page = Request::get('page', 1);
    

    You can read more in the official documentation https://laravel.com/docs/5.8/requests

    0 讨论(0)
  • 2020-12-16 09:36

    This is the best practice. This way you will get the variables from GET method as well as POST method

        public function index(Request $request) {
                $data=$request->all();
                dd($data);
        }
    //OR if you want few of them then
        public function index(Request $request) {
                $data=$request->only('id','name','etc');
                dd($data);
        }
    //OR if you want all except few then
        public function index(Request $request) {
                $data=$request->except('__token');
                dd($data);
        }
    
    0 讨论(0)
  • 2020-12-16 09:40

    In laravel 5.3

    I want to show the get param in my view

    Step 1 : my route

    Route::get('my_route/{myvalue}', 'myController@myfunction');
    

    Step 2 : Write a function inside your controller

    public function myfunction($myvalue)
    {
        return view('get')->with('myvalue', $myvalue);
    }
    

    Now you're returning the parameter that you passed to the view

    Step 3 : Showing it in my View

    Inside my view you i can simply echo it by using

    {{ $myvalue }}
    

    So If you have this in your url

    http://127.0.0.1/yourproject/refral/this@that.com

    Then it will print this@that.com in you view file

    hope this helps someone.

    0 讨论(0)
  • 2020-12-16 09:42

    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

    0 讨论(0)
提交回复
热议问题