简体   繁体   English

Laravel找不到服务

[英]Laravel can't find service

I managed to create custom validation rule following: http://www.sitepoint.com/data-validation-laravel-right-way-custom-validators/ 我设法创建了以下自定义验证规则: http : //www.sitepoint.com/data-validation-laravel-right-way-custom-validators/
My only problem is that in laravel 5 there is new file structure. 我唯一的问题是在laravel 5中有一个新的文件结构。 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. 但是,laravel无法在App \\ Providers中找不到我的ValidatorExtended.php。 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? 如何告诉laravel在App \\ Services中查看,而不在App \\ Providers中查看?

ValidatorExtended.php: 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: 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 );
          }
     } 

}

You need to specify the namespace of your ValidatorExtended accessor: 您需要指定ValidatorExtended访问ValidatorExtended的名称空间:

<?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: 或在文件顶部添加use语句:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM