Php Laravel 4自定义验证器抛出BadMethodCallException

Php Laravel 4自定义验证器抛出BadMethodCallException,php,laravel,laravel-4,Php,Laravel,Laravel 4,我正在构建我的第一个Laravel4应用程序,并尝试扩展Validator类,以简单地检查路径是否存在。接下来,我想到了这个: app/libraries/CustomValidator.php: class CustomValidator extends Illuminate\Validation\Validator { public function validatePathExists($attribute, $value, $parameters) { retur

我正在构建我的第一个Laravel4应用程序,并尝试扩展Validator类,以简单地检查路径是否存在。接下来,我想到了这个:

app/libraries/CustomValidator.php:

class CustomValidator extends Illuminate\Validation\Validator {
    public function validatePathExists($attribute, $value, $parameters) {
        return is_dir($value);
    }
}

Validator::resolver(function() {
    return new CustomValidator;
});
在控制器中(稍微精简):

此代码抛出一个
BadMethodCallException
,并显示以下消息:
方法[ValidatePatheExists]不存在。

我已将
app/libraries
添加到
composer.json
自动加载:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/libraries"
    ]
},
当然可以运行
composer dump autoload
。事实上,在相同的
app/libraries
目录下有一个Helper.php,它工作正常

另外,在app/start/global.php中(顺便说一下,我不太明白为什么我们必须(?)在两个地方有相同的autostart寄存器)

仍然会发生错误。有什么想法吗?
非常感谢您的帮助。

您没有正确注册验证程序解析程序

将下面的语法与您的代码进行比较

Validator::resolver(function($translator, $data, $rules, $messages)
{
    return new CustomValidator($translator, $data, $rules, $messages);
});

您没有任何依赖项被注入到自定义解析器中。再看一遍文档。@JosephSilber不,我甚至不认为正在使用自定义解析器。我只是注意到依赖项不存在,所以我从这里开始。@JasonLewis抱歉,我不明白你的意思。我应该补充一点,这是我第一次使用Laravel。如果您在注册解析器时查看链接到的文档,您需要为您的自定义
验证程序
类提供一些依赖项。从文档中:
返回新的CustomValidator($translator、$data、$rules、$messages)不要忘记将这些变量作为参数包含到闭包中。@JasonLewis实际上我已经尝试将它们添加到同一个异常中:
Validator::resolver(函数($translator,$data,$rules,$messages){return new CustomValidator($translator,$data,$rules,$messages);})谢谢,但我在上面回答@JasonLewis,我已经尝试过这种语法,但没有成功。
ClassLoader::addDirectories(array(

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

));
Validator::resolver(function($translator, $data, $rules, $messages)
{
    return new CustomValidator($translator, $data, $rules, $messages);
});