【问题标题】:localhost is currently unable to handle this request. HTTP ERROR 500 with Laravellocalhost 当前无法处理此请求。 Laravel 的 HTTP 错误 500
【发布时间】:2017-10-16 19:09:51
【问题描述】:

我正在尝试使用 PUT 方法编辑 JSON 格式的类别,因此我使用 guzzleHttp 库通过 laravel 5.5 解析 json 请求和响应。

当我尝试将数据抓取或插入服务器时,我的 POST、GET 方法工作正常,但 PUT 和 DELETE 方法出现错误。

我得到了两种类型的错误:

  1. localhost 当前无法处理此请求。 HTTP 错误 500

  2. 内存不足 - 致命错误:内存不足(已分配 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 合作的新手,我们将不胜感激。 如果这里有什么不清楚的地方,请建议编辑。

提前致谢!

【问题讨论】:

标签: php json http laravel-5.5 guzzle


【解决方案1】:

将您的请求更改为

$request = $this->client->request('PUT', $url,['json'=> $data]);

【讨论】:

  • 它不起作用,请检查我的编辑,表单可能有问题。
  • 不工作是什么意思,得到什么结果?
  • 我仍然收到错误:本地主机当前无法处理此请求。 HTTP 错误 500
  • 所以你有 2 个站点 1.rest-banai.herokuapp.com/categories/id ,第二个是具有问题的视图和控制器的站点,是吗?
  • rest-banai.herokuapp.com/categories/id
【解决方案2】:

您正在使用资源丰富的路线,但您的代码非常复杂。坦率地说,我认为这是完全没有必要的。我将通过以下方式完成您正在做的事情:

路线

Route::resource('category', 'CategoryController');

控制器

public function edit(Category $category)
{
    return view('category.edit', compact('category'));
}

public function update(Request $request, Category $category)
{
    try {
        $category->update($request->all()->except(['_token']));

        Session::flash('success', 'Category is successfully saved !');
    } catch(\Exception $exception) {
        Session::flash('error', 'Unable to update category!');    
    }

    return redirect(route('category.index'));
}

HTML

类别/edit.blade.php

{!! Form::open(['route' => route('categories.update', $category), '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() !!}

```

【讨论】:

  • 好的,你能具体解释一下你在哪几行中降低了处理请求的复杂性吗?我只能看到您将我的输入数组包装在一行中,仅此而已。您仍然在向存储库请求结果,并且存储库正在向 GuzzleHtttpRequest.php 请求结果......并且对于您的信息,我得到了同样的错误。
  • 参考 laravel.log 文件看看发生了什么错误。
  • 在使用资源丰富的路由时,我将这种方法用于 CRUD 操作。脂肪更容易实现。
猜你喜欢
  • 2020-09-16
  • 1970-01-01
  • 2022-06-24
  • 2017-03-06
  • 2018-03-22
  • 2017-12-19
  • 2020-03-01
  • 2018-06-21
相关资源
最近更新 更多