0

I'm trying to get parameters from url like this

domain/test?key=value

and in my Laravel route I defined it this way

Route::get('test', 'mController@index');

and in index() I'm doing this

public function index(Request $request)
{
    $value = $request -> query('key');
    echo $value;
}

but $value is empty and nothing is printed. What is wrong? It's not a duplicate of another question because my problem was from NGINX config file not the PHP code

Amir_P
  • 8,322
  • 5
  • 43
  • 92

4 Answers4

2

you should change your nginx config and pass the arguments

location / {
   try_files $uri $uri/ /index.php?$args;
}
Hanie Asemi
  • 1,318
  • 2
  • 9
  • 23
0
  1. Change url like this

    domain/test/value    
    
  2. Change route

    Route::get('test/{key}', 'mController@index');
    
  3. Access variable like this

    public function index(Request $request)
    {
       $value = $request->key;
       return $value;
    }
    
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
  • The function would need to be declared with $key as a variable: `public function index($key)`. `$value` would then equal `$key` – mbozwood May 08 '17 at 09:49
0

Try

public function index(Request $request)
    {
        $value = Input::get('key');
        echo $value;
    }
Sama
  • 351
  • 3
  • 14
0

You can use $request as the function parameter and $request->get('key) to get the parameter.

You also can use $_GET['key'] to get that parameter.

LF00
  • 27,015
  • 29
  • 156
  • 295
  • for the first one it's empty again and for the second one I'm getting `Undefined index: code` error – Amir_P May 08 '17 at 09:33
  • You can log the $_SERVER['REQUEST_URI'] to check the request values in your url – LF00 May 08 '17 at 09:41