我正在尝试在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 IlluminateValidationValidator {
{
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
}
}
知道为什么拉拉维尔不会加载课程吗?我认为如果添加到作曲家的类图中,它会自动执行此操作。
不知道为什么它不起作用,但我认为您应该尝试将其添加到应用程序/开始/全局.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',
));
创建新类对象后总是composer dumpautoload
。