doutongxuan1614 2015-03-11 14:00
浏览 16
已采纳

Laravel无法找到服务

I managed to create custom validation rule following: http://www.sitepoint.com/data-validation-laravel-right-way-custom-validators/
My only problem is that in laravel 5 there is new file structure. It should be:
in <?php namespace App\Providers; ValidationExtensionServiceProvider.php
in <?php namespace App\Services; ValidatorExtended.php
But laravel cant find my ValidatorExtended.php if its not in App\Providers. Error:

FatalErrorException in ValidationExtensionServiceProvider.php line 11: Class 'App\Providers\ValidatorExtended' not found

How do I tell laravel, to look in App\Services, not in App\Providers?

ValidatorExtended.php:

<?php namespace App\Services;

use Illuminate\Validation\Validator as IlluminateValidator;

class ValidatorExtended extends IlluminateValidator {

    private $_custom_messages = array(
        ....
    );

    public function __construct( $translator, $data, $rules, $messages = array(), $customAttributes = array() ) 
    {
         parent::__construct( $translator, $data, $rules, $messages, $customAttributes);

         $this->_set_custom_stuff();
    } 

     ....



}

ValidationExtensionServiceProvider.php:

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ValidationExtensionServiceProvider extends ServiceProvider {

     public function register() {}

     public function boot() {
          $this->app->validator->resolver( function( $translator, $data, $rules, $messages = array(), $customAttributes = array() ) {
              return new ValidatorExtended( $translator, $data, $rules, $messages, $customAttributes );
          }
     } 

}
  • 写回答

1条回答 默认 最新

  • douyou1937 2015-03-11 15:27
    关注

    You need to specify the namespace of your ValidatorExtended accessor:

    <?php namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    
    class ValidationExtensionServiceProvider extends ServiceProvider {
    
         public function register() {}
    
         public function boot() {
              $this->app->validator->resolver( function( $translator, $data, $rules, $messages = array(), $customAttributes = array() ) {
                  return new App\Services\ValidatorExtended( $translator, $data, $rules, $messages, $customAttributes );
              }
         } 
    }
    

    or add a use statement at the top of your file:

    <?php namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    use App\Services\ValidatorExtended;
    
    class ValidationExtensionServiceProvider extends ServiceProvider {
    
         public function register() {}
    
         public function boot() {
              $this->app->validator->resolver( function( $translator, $data, $rules, $messages = array(), $customAttributes = array() ) {
                  return new ValidatorExtended( $translator, $data, $rules, $messages, $customAttributes );
              }
         } 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序