Laravel 找不到服务
Laravel can't find service
我设法创建了以下自定义验证规则:http://www.sitepoint.com/data-validation-laravel-right-way-custom-validators/
我唯一的问题是 laravel 5 中有新的文件结构。应该是:
in <?php namespace App\Providers; ValidationExtensionServiceProvider.php
in <?php namespace App\Services; ValidatorExtended.php
但是 laravel 找不到我的 ValidatorExtended.php 如果它不在 App\Providers 中。错误:
FatalErrorException in ValidationExtensionServiceProvider.php line 11: Class 'App\Providers\ValidatorExtended' not found
如何让 laravel 在 App\Services 中查找,而不是在 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 );
}
}
}
您需要指定 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 );
}
}
}
或在文件顶部添加 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 );
}
}
}
我设法创建了以下自定义验证规则:http://www.sitepoint.com/data-validation-laravel-right-way-custom-validators/
我唯一的问题是 laravel 5 中有新的文件结构。应该是:
in <?php namespace App\Providers; ValidationExtensionServiceProvider.php
in <?php namespace App\Services; ValidatorExtended.php
但是 laravel 找不到我的 ValidatorExtended.php 如果它不在 App\Providers 中。错误:
FatalErrorException in ValidationExtensionServiceProvider.php line 11: Class 'App\Providers\ValidatorExtended' not found
如何让 laravel 在 App\Services 中查找,而不是在 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 );
}
}
}
您需要指定 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 );
}
}
}
或在文件顶部添加 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 );
}
}
}