68174

Laravel 5.7 - Override all() method in Request validation Class to validate route parameters?

Question:

I want to validate the route parameters in the Request validation class. I know this question has been asked many times before but <a href="https://stackoverflow.com/questions/30238285/laravel-5-how-to-validate-route-parameters" rel="nofollow">According to this question</a> I override all() method and I receive this error:

Class App\Http\Requests\DestroyUserRequest does not exist

I'm using Laravel 5.7.

<strong>Route:</strong>

Route::delete('/user/{userId}/delete', 'UserController@destroy')->name('user.destroy');

<strong>Controller:</strong>

<?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'); } }

<strong>DestroyUserRequest:</strong>

<?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; } }

What is wrong to override all() method?

Answer1:

The error your get seems to be quite strange. I believe the problem is here because your method signature is not the same as parent.

It should be:

public function all($keys = null) { $data = parent::all($keys); $data['userId'] = $this->route('userId'); return $data; }

because signature of Illuminate/Http/Concerns/InteractsWithInput.php is:

/** * Get all of the input and files for the request. * * @param array|mixed $keys * @return array */ public function all($keys = null)

The change was made in Laravel 5.5. You can read in <a href="https://laravel.com/docs/5.5/upgrade#upgrade-5.5.0" rel="nofollow">upgrade guide</a>:

<blockquote>

The all Method

If you are overriding the all method of the Illuminate\Http\Request class, you should update your method signature to reflect the new $keys argument:

public function all($keys = null) {

</blockquote>

Recommend

  • How to create a class extension in asp.net core?
  • Laravel 5 MethodNotAllowedHttpException PUT
  • Laravel relationships on a table with two types of flags
  • ClassNotFoundException: Attempted to load class… Symfony
  • How to populate the modified_by with the user_id of who made the update to the record using laravel
  • Does derived class' member functions inherit virtualness from base class?
  • Getting webGL error in autodesk viewer
  • Sails.js to client using sails.sockets.join and sails.sockets.broadcast doesn't return info to
  • Is there a way to clear some session data from ALL sessions?
  • PHPUnit: Expected status code 200 but received 419 with Laravel
  • PHP - Can't dynamcally instantiate class
  • Laravel include causes error: Method Illuminate\\View\\View::__toString() must not throw an exce
  • JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • How to concat Pandas dataframe columns
  • how to get username into sql trigger when multiple users signed on from asp membership
  • Laravel 4 routing not working due to .htaccess file?
  • Iron Router: How do I send data to the layout?
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Adding a button at the bottom of a table view
  • Getting last autonumber in access
  • Delete MySQLi record without showing the id in the URL
  • GridView Sorting works once only
  • bootstrap to use multiple ng-app
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • How to load view controller without button in storyboard?