【问题标题】:Laravel API isn't getting inputsLaravel API 没有得到输入
【发布时间】:2017-08-29 12:11:11
【问题描述】:

我正在laravel 中开发REST API,并应要求发布一些数据。但是在laravel 控制器中什么也没有出现。这是我的控制器代码:

try {
      $rules = [
         'type' => 'required|integer'
      ];

      $validator = Validator::make($this->inputs, $rules);
      if ($validator->passes()) {
         //code here
      } else {
         return response()->json([
                'status' => $this->BAD_REQUEST,
                    'response' => [
                        'message' => $validator->messages(),
                        'data' => $this->NO_DATA_ARRAY
                    ],
                ], $this->BAD_REQUEST);
      }
} catch (\Exception $ex) {
            return response()->json([
                'status' => $this->BAD_REQUEST,
                'response' => [
                    'message' => $ex->getMessage(),
                    'data' => $this->NO_DATA_ARRAY
                ],
            ], $this->BAD_REQUEST);
}

现在当我做print_r($this->inputs); 时,我什么也得不到。我从postman 发送请求并将数据作为form-data 发送。我也尝试过使用在线 API 测试仪发送它。结果还是一样。有什么帮助吗?

【问题讨论】:

  • 为此使用 laravel Request 对象
  • 尝试以 x-www-form-urlencoded 从邮递员发送数据。如果您没有在外部定义请求类型,则默认为“x-www-form-urlencoded”。
  • @Naincy 我也尝试过,结果相同
  • 你将在 Laravel 的 Request 对象中获取数据,而不是在 $this->inputs

标签: php rest laravel-5 postman


【解决方案1】:

来自Laravel documentation 在命名空间下面的类的顶部包含以下 use 语句。

use Illuminate\Http\Request;

控制器中的示例函数如下所示

     /**
     * Sample controller function
     * @param  Request  $request
     */
    public function update(Request $request)
    {
      // get request method 
      $method = $request->method();

     // retrieve all input as array
     $input = $request->all();
    }

【讨论】:

    猜你喜欢
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    相关资源
    最近更新 更多