Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BeginnerSoul's avatar

Validation on dynamic routes

Hello i have like this the web.php:

Route::get('/update/{version}', [App\Http\Controllers\CheckNewVersion::class, 'download'])->name('files.download');

My FormRequests looks like this:

<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Validation\ValidationException;

class DownloadRequest 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 [
            'version' => 'required|string',
        ];
    }

    public function messages()
    {
        return [
            'version.required' => 'You need to check if you gave all of the details!',
            'version.string'  => 'The version is invalid!'
        ];
    }

    /**
     * Handle a failed validation attempt.
     *
     * @param  \Illuminate\Contracts\Validation\Validator $validator
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function failedValidation(Validator $validator)
    {
        $errors = (new ValidationException($validator))->errors();

        throw new HttpResponseException(
            response()->json(['errors' => $errors], JsonResponse::HTTP_UNPROCESSABLE_ENTITY)
        );
    }
}

My controller:

public function download(DownloadRequest $request)
    {
        try {
            dd($request);
            $decryptversion = Crypt::decryptString($request->v);
            dd('public/' . $decryptversion  . '/' . $decryptversion  . '.zip');
            return Storage::disk('local')->download('public/' . $decryptversion  . '/' . $decryptversion  . '.zip');
        } catch (DecryptException $e) {
            //
        }
    }

If I remove the DownloadRequest in the form then everything works, if I add it then it doesn't. I checked with the failedValidation function what is the problem and it looks like the version part is not recognized which would be in the route the {version}.

Now the question is how I can validate the data? If I am using like this the url http://localhost/update/2 then it redirects me many times it says and giving me error 500 code.

If I write http://localhost/update/2?version=2 then it works however I don't need two times the version. So how I can make it possible to use the {version} in the FormRequest?

0 likes
1 reply
Snapey's avatar
Snapey
Best Answer
Level 122

You have to accept version into your controller method

public function download(DownloadRequest $request, $version)
{
    $decryptversion = Crypt::decryptString($version);

... but /2 does not look like an encrypted string?

2 likes

Please or to participate in this conversation.