Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 控制器或路由过滤器上的验证服务-Laravel_Php_Validation_Laravel_Filter_Controller - Fatal编程技术网

Php 控制器或路由过滤器上的验证服务-Laravel

Php 控制器或路由过滤器上的验证服务-Laravel,php,validation,laravel,filter,controller,Php,Validation,Laravel,Filter,Controller,我有一个UsersController,它通过服务验证器验证输入,如果验证失败,验证器将抛出一个ValidatorException。否则,控制器将创建用户 我想知道在调用控制器之前是否最好将验证移到路由过滤器,如果验证通过,那么请求可以继续并调用控制器的方法来创建用户。因此,如果调用控制器,则输入是有效的,只需创建用户即可 我问这个问题是为了看看是否有可能和好的方法来分离更多的逻辑和负责一项工作的控制器,即使对一项服务进行了验证,所以工作是分开的 我遵循了这个和这个教程 我的代码: 我喜欢使用

我有一个UsersController,它通过服务验证器验证输入,如果验证失败,验证器将抛出一个ValidatorException。否则,控制器将创建用户

我想知道在调用控制器之前是否最好将验证移到路由过滤器,如果验证通过,那么请求可以继续并调用控制器的方法来创建用户。因此,如果调用控制器,则输入是有效的,只需创建用户即可

我问这个问题是为了看看是否有可能和好的方法来分离更多的逻辑和负责一项工作的控制器,即使对一项服务进行了验证,所以工作是分开的

我遵循了这个和这个教程

我的代码:


我喜欢使用观察者进行验证。退房
Route::resource('users', 'UsersController', array('except' => array('create', 'edit')));

class UsersController extends \BaseController {

    protected $user;
    protected $response;
    protected $hasher;
    protected $validator;

    public function __construct(User $user,
        Response $response,
        \Illuminate\Hashing\BcryptHasher $hasher,
        \Inc\Services\Validation\UserRegistrationValidator $validator){

        $this->user = $user;
        $this->response = $response;
        $this->hasher = $hasher;
        $this->validator = $validator;

    }


    public function index()
    {   

        return $this->user->all();

    }



    public function store()
    {
        $input = Input::json();

        $this->validator->with($input->all())->validate();

        $this->user->create([
            'username' => $input->get('username'),
            'password' => $this->hasher->make($input->get('password'))
        ]);

    }



    public function show($id)
    {
        return $this->user->find($id);
    }



    public function update($id)
    {
        //
    }



    public function destroy($id)
    {
        $user = $this->user->find($id);
        $user->delete();
    }


}

class UserRegistrationValidator extends Validation{

    protected $rules = [
        'username' =>['required', 'min:3', 'max:50'],
        'password' =>['required', 'min:3']

    ];

}

abstract class Validation{

    protected $rules = array();
    protected $messages = array();
    protected $input = array();

    public function with(array $input){

        $this->input = $input;

        return $this;

    }

    public function validate(){

        $this->validator = Validator::make($this->input, $this->rules, $this->messages);

        if($this->validator->fails())
            throw new ValidationException($this->validator);
    }

}

class ValidationException extends Exception {

    protected $validator;

    public function __construct($validator){
        $this->validator = $validator;
    }

    public function getErrors(){

        return $this->validator->messages();
    }
}

App::error(function(Inc\Exceptions\Validation\ValidationException $exception, $code)
{
    $errorResponse = [
        'error' => $exception->getErrors()
    ];

    return Response::json($errorResponse, $statusCode = 422);
});