我想验证请求验证类中的路由参数。我知道这个问题之前已经被问过很多次了,但是 According to this question 我覆盖了 all()
方法,我收到了这个错误:
Class App\Http\Requests\DestroyUserRequest does not exist
我正在使用 Laravel 5.7。
路线:
Route::delete('/user/{userId}/delete', 'UserController@destroy')->name('user.destroy');
Controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\DestroyUserRequest;
use App\User;
class UserController extends Controller
{
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy(DestroyUserRequest $request)
{
User::find($request->route('userId'))->delete();
return $request->route('userId');
}
}
DestroyUserRequest:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class DestroyUserRequest 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 [
'userId' => 'integer|exists:users,id'
];
}
public function all()
{
$data = parent::all();
$data['userId'] = $this->route('userId');
return $data;
}
}
覆盖 all() 方法有什么问题?
最佳答案
你得到的错误似乎很奇怪。我相信问题出在这里,因为您的方法签名与父方法不同。
它应该是:
public function all($keys = null)
{
$data = parent::all($keys);
$data['userId'] = $this->route('userId');
return $data;
}
因为
Illuminate/Http/Concerns/InteractsWithInput.php
的签名是:/**
* Get all of the input and files for the request.
*
* @param array|mixed $keys
* @return array
*/
public function all($keys = null)
更改是在 Laravel 5.5 中进行的。您可以在 upgrade guide 中阅读:
关于Laravel 5.7 - 覆盖请求验证类中的 all() 方法以验证路由参数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52402139/