我的请求文件在 api 路由上出现 NotFoundHttpException
NotFoundHttpException with my Request file on api route
我在 api 文件中创建了一个路由,允许每个人创建用户:
Route::post('users', 'UserController@addUser');
当我在 postman 中调用它而不使用请求验证时,它起作用了。但是当我创建我的请求文件并使用它时,Laravel return 一个 NotFoundHttpException。
这是我的请求文件:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserAddRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'user_id' => 'required|numeric',
'name' => 'required|string',
'email' => 'required|string',
'password' => 'required|string'
];
}
}
public function addUser(UserAddRequest $request){
$user = new User;
$user->instance_user_id = $request->input('user_id');
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = $request->input('password');
$user->save();
}
没有表格,因为需要用post方法直接发送到服务器。我在 routes/api.php 中声明了我的路线,我用 url /api/users 调用它 api 在这种情况下不需要检查凭据。
我解决了我的问题:
引发了 NotFoundHttpException,因为我没有正确发送我的参数并且 Laravel 没有将我重定向回来。当我直接向服务器发送请求时,没有为我要去的地方声明 url。
我在 api 文件中创建了一个路由,允许每个人创建用户:
Route::post('users', 'UserController@addUser');
当我在 postman 中调用它而不使用请求验证时,它起作用了。但是当我创建我的请求文件并使用它时,Laravel return 一个 NotFoundHttpException。
这是我的请求文件:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserAddRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'user_id' => 'required|numeric',
'name' => 'required|string',
'email' => 'required|string',
'password' => 'required|string'
];
}
}
public function addUser(UserAddRequest $request){
$user = new User;
$user->instance_user_id = $request->input('user_id');
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = $request->input('password');
$user->save();
}
没有表格,因为需要用post方法直接发送到服务器。我在 routes/api.php 中声明了我的路线,我用 url /api/users 调用它 api 在这种情况下不需要检查凭据。
我解决了我的问题:
引发了 NotFoundHttpException,因为我没有正确发送我的参数并且 Laravel 没有将我重定向回来。当我直接向服务器发送请求时,没有为我要去的地方声明 url。