我正在测试我的API,因此我想创建请求,但我无法让params工作。我所做的就是添加一个Hello指向World的param,但是当我尝试获取该参数时Hello返回NULL。缺少什么?这是代码:
//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"}
//怎么样&#34; NULL&#34;?
答案 0 :(得分:3)
添加“Request :: replace($ request-&gt; input());”解决了我的问题,我不明白为什么:
$request = Request::create('/json/locations', 'GET', array('Hello' => 'World'));
Request::replace($request->input());
return Route::dispatch($request)->getContent();
无论如何,它现在有效。忘了在这里更新问题。
答案 1 :(得分:-1)
要在Laravel中获取GET或POST参数,您应该使用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);
}
}