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 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统
  • ¥15 快手联盟怎么快速的跑出建立模型