laravel 不能使用类,因为它不是特征

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23553160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:30:00  来源:igfitidea点击:

cannot use class because it is not a trait

phplaravellaravel-4

提问by arrowill12

I have followed the tutorial hereon building a validation service for laravel. I am having issues now when trying to call the validator from one of my controllers. I am seeing the error:

我已经按照这里的教程为 laravel 构建验证服务。我现在在尝试从我的一个控制器调用验证器时遇到问题。我看到错误:

validController cannot use Portal\Service\Validation\Laravel\AppInstancesValidator - it is not a trait

validController cannot use Portal\Service\Validation\Laravel\AppInstancesValidator - it is not a trait

here is my controller:

这是我的控制器:

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();
  }

}

and my validator:

和我的验证器:

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'
  );

}

回答by Sam

Try putting the usebefore the classdeclaration:

尝试将 放在声明use之前class

<?php // namespace Portal\Controllers;

use \Portal\Service\Validation\Laravel\AppInstancesValidator;

class validController extends BaseController {

    public function validateInstance() {}

}

回答by c-griffin

Your 'use' statement should be above the class definition of validController

您的“使用”语句应位于 validController 的类定义之上