2

我正在尝试在 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 不会加载课程?我认为如果添加到作曲家的类图中它会自动完成。

4

2 回答 2

1

总是composer dumpautoload在创建新的类对象之后做。

于 2013-12-15T08:02:07.733 回答
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',

));
于 2013-10-25T23:15:31.193 回答