cannot use class because it is not a trait

佐手、 提交于 2019-12-05 18:47:01

问题


I have followed the tutorial here on 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:

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

}

回答1:


Try putting the use before the class declaration:

<?php // namespace Portal\Controllers;

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

class validController extends BaseController {

    public function validateInstance() {}

}



回答2:


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



来源:https://stackoverflow.com/questions/23553160/cannot-use-class-because-it-is-not-a-trait

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!