【发布时间】:2013-03-28 01:24:04
【问题描述】:
我是 laravel 新手,我正在尝试实现一个简单的 rest api。
我已经实现了控制器,并通过单元测试进行了测试。
我的问题在于 POST 请求。
通过测试 Input:json 有数据,通过外部 rest 客户端返回 null。
这是单元测试的代码
$newMenu = array(
'name'=>'Christmas Menu',
'description'=>'Christmas Menu',
'img_url'=>'http://www.example.com',
'type_id'=>1,
);
Request::setMethod('POST');
Input::$json = $newMenu;
$response = Controller::call('menu@index');
我做错了什么?
更新:
这真的让我发疯了
我已经实例化了一个新的 laravel 项目,并且只有以下代码:
路线
Route::get('test', 'home@index');
Route::post('test', 'home@index');
控制器:
class Home_Controller extends Base_Controller {
public $restful = true;
public function get_index()
{
return Response::json(['test'=>'hello world']);
}
public function post_index()
{
return Response::json(['test'=>Input::all()]);
}
}
CURL 调用:
curl -H "Accept:application/json" -H"Content-type: application/json" -X POST -d '{"title":"world"}' http://localhost/laravel-post/public/test
回复:
{"test":[]}
谁能指出哪里出了问题。
这真的让我无法使用 laravel,我真的很喜欢这个概念。
【问题讨论】:
-
你在浏览器中试过了吗?只是为了确保你的 curl 调用不是问题?
-
我用rest客户端(CocoaRest Client)试过了,但是我还没有创建gui,所以还没有浏览器测试