How to validate Route Parameters in Laravel 5?

2019-03-24 18:09发布

问题:

As you know Laravel 5 changes the way you call the validator, the old way is calling the validator facade, but now there is the ValidatesRequests trait in base Controller class, but the validate method accepts the request as the values array, but when you define your route parameters, these values are not stored in Request, so how can I validate those parameters ?

Edit:

Route:

Route::get('/react-api/{username}', 'ProfileController@getUsername');

Controller:

public function getUsername(Request $request, $username)
{
     $v = $this->validate($request, ['username' => 'required']);
}

So, the question how can i validate this username parameter ?

回答1:

Manix's answer wasn't working for me, I was having the same issues as Iliyass. The issue is route parameters aren't automatically available to the FormRequest. I ended up overriding the all() function in my particular FormRequest Class:

public function all()
{
    // Include the next line if you need form data, too.
    $request = Input::all();
    $request['username'] = $this->route('username');
    return $request
}

Then you can code rules as normal:

public function rules()
{
    return [
        'username' => 'required',
    ];
}


回答2:

public function listTurns($doctor_id, $limit, $offset){
        $r = [
            'doctor_id' => $doctor_id,
            'limit' => $limit,
            'offset' => $offset,
        ];

        $validator = Validator::make($r, [
            'doctor_id' => 'required|numeric|min:1|exists:doctors,id',
            'limit' => 'required|numeric|min:1',
            'offset' => 'required|numeric|min:0',
        ]);
}


回答3:

Supposue the following route:

Route::get('profile/{id}', 'ProfileController@show');

You can still validate id parameter as L4 way:

public function show(){
    $validator = \Validator::make(
        \Input::all(),
        [
             'id' => ['required', 'numeric']
        ]
    );

    // run validator here
}

If you need to validate concrete data, take a look the following example:

public function getUsername(Request $request, $username)
{
    $validator = \Validator::make(
        [
             'username' => $username
        ],
        [
             'username' => ['required']
        ]
    );

    // run the validator here
}

L5 let you do in two other ways. The first one, using a generic Request class injected in the controller:

public function show(Request $request){
    $this->validate($request, [
        'id' => ['required', 'numeric']
    ]);

    // do stuff here, everything was ok
}

In L5 you are allowed to call validate() functions that receive the request and the rules to run over it. This functions is in charge of run rules, if some rule fails, then the user is redirected to previous request

Finally, as second option, you can use Form request validation. Remember, every GET and POST value can be accessed via Request class



回答4:

use Validator;

public function getUsername($username)
{
    $validator = Validator::make(['username' => $username], [
      'username' => 'required|string'
    ]);

    if ($validator->fails()) {
      return response()->json(['success' => false, 'errors' => $validator->messages()], 422);
    }
}