Home > front end >  How to use namespace in Laravel in controller?
How to use namespace in Laravel in controller?

Time:06-14

I have a custom class in App/Helpers/regexValidation.php:

<?

namespace App\Helpers;

class RegexValidation
{

    public static function isCVC($str)
    {
        $re = '/^[0-9]{3,4}$/s';

        preg_match($re, $str, $matches);

        return $matches;
    }
}

I try to use it in controller:

<?php

namespace App\Http\Controllers;

use App\Helpers\RegexValidation;

public function detectfile(Request $request)
{
   $cvc = RegexValidation::isCVC(555);
}

When I call this method from route Laravel I get:

Error: read ECONNRESET

If comment this line $cvc = RegexValidation::isCVC(555); it works.

What do I do wrong?

CodePudding user response:

I think you are having a PSR4 Error,

I have a custom class in App/Helpers/regexValidation.php

Rename that file to PascalCase as RegexValidation.php

after renaming run

composer dumpautoload

And it should work.

  • Related