【发布时间】:2017-10-16 19:09:51
【问题描述】:
我正在尝试使用 PUT 方法编辑 JSON 格式的类别,因此我使用 guzzleHttp 库通过 laravel 5.5 解析 json 请求和响应。
当我尝试将数据抓取或插入服务器时,我的 POST、GET 方法工作正常,但 PUT 和 DELETE 方法出现错误。
我得到了两种类型的错误:
localhost 当前无法处理此请求。 HTTP 错误 500
内存不足 - 致命错误:内存不足(已分配 1472200704)(尝试分配 176128 字节)
控制台错误:
如果我连续两次请求,这些错误不会一个接一个地发生。
我试图更改分配的内存,但没有成功!
这是我处理请求的程序:
我的控制器:
public function update(Request $request, $id)
{
// get the inputs
$inputs = [
"cat_id" => $id,
"cat_name" => $request->input('cat_name'),
"cat_slug" => $request->input('cat_slug'),
"cat_description" => $request->input('cat_description'),
"cat_parent_id" => $request->input('cat_parent_id')
];
// post to the database
$response = $this->categories->update($id, $inputs);
if($response['success']){
$message = $response['message'];
Session::flash('success', 'Category is successfully saved !'.' Server Response Message : '.$message);
return redirect()->route('categories.index');
}else{
$message = $response['message'];
Session::flash('success', 'Category is not successfully saved !'.' Server Response Message : '.$message);
return redirect()->route('categories.index');
}
/////////////////////////////////////////////////
// If the edit page should be shown
//return redirect()->route('categories.edit', $id);
}
我的存储库:
public function update($id, $category){
return $this->update("categories/{$id}", $category);
}
还有我的自定义 GuzzleHttpRequest.php:
protected function update($url, $data){
$formatted_data = json_encode($data);
$request = $this->client->request('PUT', $url, [
'body' => $formatted_data
]);
$response = json_decode( $request->getBody()->getContents() , true );
return $response;
}
我的服务器接受 JSON 格式的输入:https://rest-banai.herokuapp.com/
已编辑:
还有我的编辑表单:
{!! Form::open(['route' => ['categories.update', $category['cat_id']], 'method' => 'PUT', 'data-parsley-validate' => '']) !!}
<div class="form-group">
{{ Form::label('cat_name', 'Name:') }}
{{ Form::text('cat_name', $category['cat_name'], ['class' => 'form-control', 'placeholder' => 'Enter category name ...', 'required' => '', 'maxlength' => '50']) }}
</div>
<div class="form-group">
{{ Form::label('cat_slug', 'Slug:') }}
{{ Form::text('cat_slug', $category['cat_slug'], ['class' => 'form-control', 'placeholder' => 'Enter a slug word ...', 'required' => '', 'maxlength' => '50', 'data-parsley-type' => 'alphanum']) }}
</div>
<div class="form-group">
{{ Form::label('cat_description', 'Description:') }}
{{ Form::textarea('cat_description', $category['cat_description'], ['class' => 'form-control', 'rows' => '3', 'placeholder' => 'Enter description of the category ...', 'maxlength' => '255']) }}
</div>
<div class="form-group">
{{ Form::label('cat_parent_id', 'Parent Category:') }}
<br />
{{ Form::select('cat_parent_id', $cat_array, null, ['placeholder' => $cat_parent_name]) }}
<br />
</div>
<div class="pull-right">
{{ Form::submit('SAVE', ['class' => 'btn btn-block btn-success btn-sm']) }}
</div>
{!! Form::close() !!}
我不确定我在这里做错了什么,任何专家,请帮助我解决这个问题,因为我是 Guzzle 和 JSON 与 Laravel 合作的新手,我们将不胜感激。 如果这里有什么不清楚的地方,请建议编辑。
提前致谢!
【问题讨论】:
-
更新地址在你的服务器上吗?
-
更新 url 为:rest-banai.herokuapp.com/categories/id,我已将 guzzle 客户端 base_uri 设置为 rest-banai.herokuapp.com,然后将类别/id 传递给更新
-
@madalinivascu 你能告诉我 $request 部分的语法吗?是 header var 还是 body var?
标签: php json http laravel-5.5 guzzle