我已经按照教程here来为laravel构建验证服务。我试图从我的一个控制器调用验证器时遇到问题。我看到了错误:
validController cannot use Portal\Service\Validation\Laravel\AppInstancesValidator - it is not a trait
这是我的控制器:
class validController extends BaseController {
use \Portal\Service\Validation\Laravel\AppInstancesValidator;
public function validateInstance() {
$post = Input::all();
$instVal = new AppInstancesValidator( App::make('validator'));
return $instVal->with($post)->passes();
}
}
和我的验证员:
namespace Portal\Service\Validation\Laravel;
use Portal\Service\Validation\ValidableInterface;
class AppInstancesValidator extends LaravelValidator implements ValidableInterface {
protected $rules = array(
'app_name' => 'required',
'app_instance_name' => 'required',
'app_instance_ip' => 'required|ip'
);
}
答案 0 :(得分:22)
尝试将use
放在class
声明之前:
<?php // namespace Portal\Controllers;
use \Portal\Service\Validation\Laravel\AppInstancesValidator;
class validController extends BaseController {
public function validateInstance() {}
}
答案 1 :(得分:1)
您的使用&#39;语句应该高于validController的类定义