不能使用类因为它不是特质

时间:2014-05-08 21:48:30

标签: php laravel laravel-4

我已经按照教程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'
  );

}

2 个答案:

答案 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的类定义