Rkey
Rkey

Reputation: 700

Laravel: Can't add params to request

I'm testing my API so I want to create requests, but I can't get the params to work. All I do is to add a param with Hello pointing to World, but Hello returns NULL when I try to get that parameter. What's missing? Here's the code:

//This is routes.php:
<?php 
Route::get('/testapi', 'TestController@testapi');
Route::get('/json/locations', 'APILocationController@getForUser');
?>


//This is TestController.php:
<?php

class TestController extends \BaseController {
    public function testapi() {
        echo 'Create the request';
        echo '<br>';
        $request = Request::create('/json/locations', 'GET', array('Hello' => 'World'));
        return Route::dispatch($request)->getContent();
    }
}
?>


//This is APILocationController.php:
<?php

class APILocationController extends \BaseController {
    public function getForUser() {
        echo var_dump(Request::get('Hello'));
        echo '<br>';
        return Response::json(array('message' => 'Index all locations based on User'), 200);
    }
}
?>

//This is the output:
Create the request
NULL 
{"message":"Index all locations based on User"}

//How is that "NULL"?

Upvotes: 2

Views: 2931

Answers (2)

Rkey
Rkey

Reputation: 700

Adding a "Request::replace($request->input());" solved my problem, I have no idea why:

    $request = Request::create('/json/locations', 'GET', array('Hello' => 'World'));
    Request::replace($request->input());
    return Route::dispatch($request)->getContent();

Anyway, it works now. Forgot to update the question here.

Upvotes: 3

Jerodev
Jerodev

Reputation: 33186

To get GET or POST parameters in Laravel, you should use Input.

<?php

class APILocationController extends \BaseController {
    public function getForUser() {
        echo var_dump(Input::get('Hello'));
        echo '<br>';
        return Response::json(array('message' => 'Index all locations based on User'), 200);
    }
}

Upvotes: -1

Related Questions