Eric Evans
Eric Evans

Reputation: 670

Class not found after adding Service Provider class

I'm trying to extend the Validator class. So that my text field doesn't return false when space is added.

I'm create a folder like app\extension\validation

Here's my CustomValidation class

<?php namespace App\Extension\Validation;

 use Illuminate\Validation\Validator;

 class CustomValidation extends Validator{

   public function alpha_spaces($attribute, $value, $parameters)
   {
    if(preg_match("/^([-a-z0-9_ ])+$/i", $value))
    {
        return true;
    }
    return false;
   }
 }

Here's the ValidationServiceProvider class in the same folder

<?php namespace App\Extension\Validation;

use Illuminate\Support\ServiceProvider;

class ValidationServiceProvider extends ServiceProvider{

    public function register(){}

    public function boot()
    {
        $this->app->validator->resolver(function($translator, $data, $rules, $messages)
        {
            return new CustomValidation($translator, $data, $rules, $messages);
        });
    }
}

And here is the error that I'm getting when I try to add the service provider to app.php

Class 'App\Extension\Validation\ValidationServiceProvider' not found

I have the class in app\Extension\Validation as ValidationServiceProvider.php

Does anyone have any idea why I'm having this problem. I've been stuck on this all day and I need help from someone please.

Upvotes: 4

Views: 2984

Answers (1)

Chen-Tsu Lin
Chen-Tsu Lin

Reputation: 23234

You need to add folder "app/Extension" to composer.json's autoload > classmap property like below:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/Extension"
    ]
} 

After do this, composer knows it should load class from "app/Extension"

And then execute

composer dump-autoload

Upvotes: 3

Related Questions