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

cemcminn's avatar

Questions about Practical Validating/Sanitizing solution(s)

  1. If I use a class created with 'php artisan make:request' to record my validation rules, am I able to only have one 'request class' in my project? If not, I can't find the right idea of how to make multiple request classes to handle my various resource controllers. How is this usually done?

  2. Now the part of my question that relates to sanitizing inputs (with filter_var):

I saw a sample where someone added a sanitize method as a separate method in the request class and then validated the sanitized data with the 'rules' after it was sanitized. Is this a preferred method in Laravel?

Thank you.

0 likes
6 replies
jlrdw's avatar

I have a helper class and I use strip_tags:

    public static function fixValue($rvalue)
    {
        $rvalue = empty($rvalue) && !is_numeric($rvalue) ? NULL : trim(strip_tags($rvalue));
        return $rvalue;
    }

cemcminn's avatar

@jlrdw Ok. I just went to research 'best ways to make/use helper files in Laravel'. I had several really good helper files in my native PHP code base but I feel like that now I am trying to learn Laravel, I need to question everything again to learn the 'best' way to do things (hence all the questions).

So how do you access the helper files? In the routes file closure or as a trait or something else?

Thank you again.

jlrdw's avatar

My helpers are static, so a use statement

use App\Helpers\Clnsantize as Cln;

And usage:

$comments = Cln::fixValue(Request::input('comments'));

Still validate, and note numeric and date doesn't need strip_tags, if that's what the field types are in MySql.

If actually storing some HTML like the forum does, there's html purifiers on Github for laravel.

I deal with business apps, no code is stored, I always use strip_tags.

Many here will argue against static methods, look in vendor and see how Taylor uses static often, not everything, but helpers are a good choice for static methods.

But for other general things, I also have a services folder, example Length aware paginator helper:

<?php

namespace App\Services;

use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;

abstract class LengthPager
{

    /**
     * Create paginator
     *
     * @param  Illuminate\Support\Collection  $collection
     * @param  int     $total
     * @param  int     $perPage
     * @return string
     */
    public static function makeLengthAware($collection, $total, $perPage, $appends = null, $pageclass = null)
    {
        //$p = new LengthAwarePaginator($perPage, $total, $perPage, $currentPage, $options);
        
        $paginator = new LengthAwarePaginator(
                $collection, $total, $perPage, Paginator::resolveCurrentPage(), ['path' => Paginator::resolveCurrentPath()]
        );

        if ($appends) {
            $paginator->appends($appends);
        }
       return str_replace('/?', '?', $paginator->render($pageclass));
       
    }

}




1 like
Snapey's avatar

You can make as many request classes as you need. Your only limitation is thinking of unique names for them

1 like
cemcminn's avatar

@SNAPEY - @snapey Lol! I finally figured that out and I am hoping I can keep the naming schema to something similar to the Controller(s) names it may be assigned to.

For those who may be searching for this in the future, on your 'Insert' and 'Update' methods, make sure you type hint your parameter to the same name as your Request file. In other words, you might normally type hint to 'Request $request', now it will be something like 'MyControllerRequest $request' (if 'MyControllerRequest' is what you named your Request class). It's in the instructions - it just took me a bit of time to get there and find it.

cemcminn's avatar

@JLRDW - Great info! I need this info for a few other things.

My issue won't be HTML sanitizing input so much as just validating/sanitizing normal business data entry (from both public and administrators). I found waavi/Sanitizer. I plan to use it for the base sanitizing, but it looks like I will have to add a couple of custom items -- however, that part looks fairly painless in the samples on the github page.

Thank you!

Please or to participate in this conversation.