Laravel 5 __construct() 参数传递错误
Laravel 5 __construct() argument passing error
所以我试图将我所有的验证规则分组到文件夹中的相应文件中,以便于维护。下面是我的文件夹结构:
Project
--app
--config
--(more folders)
--domains
----App
--------Entities
--------Repositories
--------Services
--------Validators
----Core
--------Validators
所以我想要实现的是 Core\Validators 我创建了一个 LaravelValidator.php 看起来像这样
<?php namespace Core\Validators;
use Validator;
abstract class LaravelValidator {
/**
* Validator
*
* @var \Illuminate\Validation\Factory
*/
protected $validator;
/**
* Validation data key => value array
*
* @var Array
*/
protected $data = array();
/**
* Validation errors
*
* @var Array
*/
protected $errors = array();
/**
* Validation rules
*
* @var Array
*/
protected $rules = array();
/**
* Custom validation messages
*
* @var Array
*/
protected $messages = array();
public function __construct(Validator $validator)
{
$this->validator = $validator;
}
/**
* Set data to validate
*
* @return \Services\Validations\AbstractLaravelValidator
*/
public function with(array $data)
{
$this->data = $data;
return $this;
}
/**
* Validation passes or fails
*
* @return Boolean
*/
public function passes()
{
$validator = Validator::make(
$this->data,
$this->rules,
$this->messages
);
if ($validator->fails())
{
$this->errors = $validator->messages();
return false;
}
return true;
}
/**
* Return errors, if any
*
* @return array
*/
public function errors()
{
return $this->errors;
}
}
然后在我的 App\Validators 中创建了一个文件名 RegistrationFormValidator.php 看起来像这样
<?php namespace App\Validators\Profile;
class RegistrationFormValidator extends \Core\Validators\LaravelValidator
{
protected $rules = array(
'first_name' => 'required',
'last_name' => 'required',
'username' => 'required',
'password' => 'required',
'rTPassword' => 'required',
'profile_url' => 'required',
'email' => 'required|email',
'gender' => 'required',
'dob' => 'required',
);
}
所以通常在 laravel 4.2 中,为了验证某些事情,我所做的就是构建验证规则,然后在服务中调用它,看起来像这样
<?php namespace App\Services\Profile;
/*
|-----------------------------------------------------------
| This section injects the repositories being used
| in this service.
|-----------------------------------------------------------
*/
use App\Repositories\Profile\ProfileRepository;
use Core\ValidationFailedException;
use App\Validators\Profile\RegistrationFormValidator;
use Validator;
class ProfileService implements ProfileServiceInterface
{
protected $_profile;
protected $v;
/*
|-----------------------------------------------------------
| All construsted models variables must carry
| the '_' sign to identify it as a model variable
|-----------------------------------------------------------
*/
public function __construct(ProfileRepository $_profile, RegistrationFormValidator $v)
{
$this->_profile = $_profile;
$this->v = $v;
}
/*
|-----------------------------------------------------------
| 1. All try and catch error handling must be done
| in the respective controllers.
|
| 2. All data formattings must be done in this section
| then pass to repository for storing.
|
| 3. No controller actions allown in this section
|-----------------------------------------------------------
*/
public function createProfile($array)
{
if($this->v->passes())
{
//save into db
}
else
{
throw new ValidationFailedException(
'Validation Fail',
null,
$this->v->errors()
);
}
}
}
但问题是一旦我升级到 laravel 5 我做了同样的事情,当我尝试执行代码时它 returns 我遇到了这个错误
ErrorException in ProfileService.php line 26:
Argument 2 passed to App\Services\Profile\ProfileService::__construct() must be an instance of App\Validators\Profile\RegistrationFormValidator, none given
我的代码在 L4.2 中工作得非常好,但是一旦我升级它就不再工作了。我也知道我可以像这样进行验证
public function createProfile($array)
{
$v = Validator::make($array, [
'first_name' => 'required',
'last_name' => 'required',
'username' => 'required',
'password' => 'required',
'rTPassword' => 'required',
'profile_url' => 'required',
'email' => 'required|email',
'gender' => 'required',
'dob' => 'required',
]);
if($v->passes())
{
}
else
{
throw new ValidationFailedException(
'Validation Fail',
null,
$v->errors()
);
}
}
但问题是,如果我有更多的验证规则或场景,它将淹没整个服务文件。
有什么建议或解决方案可以指导我吗?提前致谢!
在 Laravel 5 中,您有类似的东西,它可以更好地处理验证并使验证变得干净简单。它被称为Form Request Validation。想法是一样的——有不同的 classes 来处理不同场景中的验证。
因此,只要您需要验证,就可以创建新的 FormRequest,如下所示:
php artisan make:request RegisterFormRequest
将在app/Http/Requests
下生成一个新的class。在那里你可以看到它有两个方法 authorize
和 rules
。在第一个中,您可以检查是否允许给定用户提出此请求。在第二种方法中,您可以定义规则,就像在验证器中一样。
public functions rules() {
return array(
'first_name' => 'required',
'last_name' => 'required',
'username' => 'required',
'password' => 'required',
'rTPassword' => 'required',
'profile_url' => 'required',
'email' => 'required|email',
'gender' => 'required',
'dob' => 'required',
);
}
然后你可以像这样改变你的控制器方法:
public function postCreateProfile(RegisterFormRequest $request) {
// your code here
}
这里有一些很酷的东西。第一个 - class 将由 IoC 容器自动构造并注入到您的控制器方法中,您不需要做一些特殊的事情。第二件很酷的事情是,验证检查是在将 Request 对象传递给控制器之前完成的,因此如果发生任何验证错误,您将根据您的规则集被重定向回所有错误。这意味着在 postCreateProfile
方法中编写您的代码,您可以假设如果此代码被执行,则验证已在该位置通过,您不需要额外的检查。
我建议您迁移代码以使用 Laravel 5 Form Requests,因为您需要的已经在框架中实现,是的,基本上这就是从一个版本迁移到另一个版本的要点。您还可以查看 documentation 以获取更多示例。
所以我试图将我所有的验证规则分组到文件夹中的相应文件中,以便于维护。下面是我的文件夹结构:
Project
--app
--config
--(more folders)
--domains
----App
--------Entities
--------Repositories
--------Services
--------Validators
----Core
--------Validators
所以我想要实现的是 Core\Validators 我创建了一个 LaravelValidator.php 看起来像这样
<?php namespace Core\Validators;
use Validator;
abstract class LaravelValidator {
/**
* Validator
*
* @var \Illuminate\Validation\Factory
*/
protected $validator;
/**
* Validation data key => value array
*
* @var Array
*/
protected $data = array();
/**
* Validation errors
*
* @var Array
*/
protected $errors = array();
/**
* Validation rules
*
* @var Array
*/
protected $rules = array();
/**
* Custom validation messages
*
* @var Array
*/
protected $messages = array();
public function __construct(Validator $validator)
{
$this->validator = $validator;
}
/**
* Set data to validate
*
* @return \Services\Validations\AbstractLaravelValidator
*/
public function with(array $data)
{
$this->data = $data;
return $this;
}
/**
* Validation passes or fails
*
* @return Boolean
*/
public function passes()
{
$validator = Validator::make(
$this->data,
$this->rules,
$this->messages
);
if ($validator->fails())
{
$this->errors = $validator->messages();
return false;
}
return true;
}
/**
* Return errors, if any
*
* @return array
*/
public function errors()
{
return $this->errors;
}
}
然后在我的 App\Validators 中创建了一个文件名 RegistrationFormValidator.php 看起来像这样
<?php namespace App\Validators\Profile;
class RegistrationFormValidator extends \Core\Validators\LaravelValidator
{
protected $rules = array(
'first_name' => 'required',
'last_name' => 'required',
'username' => 'required',
'password' => 'required',
'rTPassword' => 'required',
'profile_url' => 'required',
'email' => 'required|email',
'gender' => 'required',
'dob' => 'required',
);
}
所以通常在 laravel 4.2 中,为了验证某些事情,我所做的就是构建验证规则,然后在服务中调用它,看起来像这样
<?php namespace App\Services\Profile;
/*
|-----------------------------------------------------------
| This section injects the repositories being used
| in this service.
|-----------------------------------------------------------
*/
use App\Repositories\Profile\ProfileRepository;
use Core\ValidationFailedException;
use App\Validators\Profile\RegistrationFormValidator;
use Validator;
class ProfileService implements ProfileServiceInterface
{
protected $_profile;
protected $v;
/*
|-----------------------------------------------------------
| All construsted models variables must carry
| the '_' sign to identify it as a model variable
|-----------------------------------------------------------
*/
public function __construct(ProfileRepository $_profile, RegistrationFormValidator $v)
{
$this->_profile = $_profile;
$this->v = $v;
}
/*
|-----------------------------------------------------------
| 1. All try and catch error handling must be done
| in the respective controllers.
|
| 2. All data formattings must be done in this section
| then pass to repository for storing.
|
| 3. No controller actions allown in this section
|-----------------------------------------------------------
*/
public function createProfile($array)
{
if($this->v->passes())
{
//save into db
}
else
{
throw new ValidationFailedException(
'Validation Fail',
null,
$this->v->errors()
);
}
}
}
但问题是一旦我升级到 laravel 5 我做了同样的事情,当我尝试执行代码时它 returns 我遇到了这个错误
ErrorException in ProfileService.php line 26:
Argument 2 passed to App\Services\Profile\ProfileService::__construct() must be an instance of App\Validators\Profile\RegistrationFormValidator, none given
我的代码在 L4.2 中工作得非常好,但是一旦我升级它就不再工作了。我也知道我可以像这样进行验证
public function createProfile($array)
{
$v = Validator::make($array, [
'first_name' => 'required',
'last_name' => 'required',
'username' => 'required',
'password' => 'required',
'rTPassword' => 'required',
'profile_url' => 'required',
'email' => 'required|email',
'gender' => 'required',
'dob' => 'required',
]);
if($v->passes())
{
}
else
{
throw new ValidationFailedException(
'Validation Fail',
null,
$v->errors()
);
}
}
但问题是,如果我有更多的验证规则或场景,它将淹没整个服务文件。
有什么建议或解决方案可以指导我吗?提前致谢!
在 Laravel 5 中,您有类似的东西,它可以更好地处理验证并使验证变得干净简单。它被称为Form Request Validation。想法是一样的——有不同的 classes 来处理不同场景中的验证。
因此,只要您需要验证,就可以创建新的 FormRequest,如下所示:
php artisan make:request RegisterFormRequest
将在app/Http/Requests
下生成一个新的class。在那里你可以看到它有两个方法 authorize
和 rules
。在第一个中,您可以检查是否允许给定用户提出此请求。在第二种方法中,您可以定义规则,就像在验证器中一样。
public functions rules() {
return array(
'first_name' => 'required',
'last_name' => 'required',
'username' => 'required',
'password' => 'required',
'rTPassword' => 'required',
'profile_url' => 'required',
'email' => 'required|email',
'gender' => 'required',
'dob' => 'required',
);
}
然后你可以像这样改变你的控制器方法:
public function postCreateProfile(RegisterFormRequest $request) {
// your code here
}
这里有一些很酷的东西。第一个 - class 将由 IoC 容器自动构造并注入到您的控制器方法中,您不需要做一些特殊的事情。第二件很酷的事情是,验证检查是在将 Request 对象传递给控制器之前完成的,因此如果发生任何验证错误,您将根据您的规则集被重定向回所有错误。这意味着在 postCreateProfile
方法中编写您的代码,您可以假设如果此代码被执行,则验证已在该位置通过,您不需要额外的检查。
我建议您迁移代码以使用 Laravel 5 Form Requests,因为您需要的已经在框架中实现,是的,基本上这就是从一个版本迁移到另一个版本的要点。您还可以查看 documentation 以获取更多示例。