自定义验证类未在Laravel 4中加载

时间:2013-10-25 23:09:19

标签: php validation laravel laravel-4 composer-php

我正在尝试在Laravel 4中编写自己的验证类。为此,我创建了一个名为app/validators的新目录。然后我将此目录添加到composer.json类图并运行composer update,如下所示:

"classmap": [
    "app/commands",
    "app/controllers",
    "app/models",
    "app/validators",           <- added here
    "app/database/migrations",
    "app/database/seeds",
    "app/tests/TestCase.php"
]

我的验证课如下:

class LinkValidation extends Illuminate\Validation\Validator {
{
    public function validateHost($field, $value, $params)
    {
        return $value == 'test';
    }
}

在我的控制器中,我试图像这样扩展验证器:

Validator::extend('awesome', 'LinkValidation@supportedHost');

但是,我在加载页面时遇到此错误:

{
    "error":{
        "type":"ReflectionException",
        "message":"Class LinkValidation does not exist",
        "file":"C:\\xampp\\htdocs\\laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php",
        "line":301
    }
}

为什么Laravel不会加载课程?如果添加到作曲家的类图中,我认为它会自动执行。

2 个答案:

答案 0 :(得分:1)

不确定为什么它不起作用,但我认为你应该尝试在 app / start / global.php 中添加它。

/*
|--------------------------------------------------------------------------
| Register The Laravel Class Loader
|--------------------------------------------------------------------------
|
| In addition to using Composer, you may use the Laravel class loader to
| load your controllers and models. This is useful for keeping all of
| your classes in the "global" namespace without Composer updating.
|
*/

ClassLoader::addDirectories(array(

    app_path().'/commands',
    app_path().'/controllers',
    app_path().'/models',
    app_path().'/database/seeds',
    app_path().'/validators',

));

答案 1 :(得分:1)

创建新的类对象后始终执行composer dumpautoload