Laravel 5 __construct() argument passing error

北城以北 提交于 2019-12-06 04:18:56

In Laravel 5 you have something similar, which handles better the validation and makes validation clean and easy. It is called Form Request Validation. The idea there is the same - to have different classes that handle validation in different scenarios.

So whenever you need a validation you can create new FormRequest, like this:

php artisan make:request RegisterFormRequest

A new class will be generated under app/Http/Requests. There you can see it has two methods authorize and rules. In the first one you can make a check if given user is allwed to make this request. In the second method you can define your rules, just like in the validator.

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',
    );
}

Then you can change your controller method like this:

public function postCreateProfile(RegisterFormRequest $request) {
    // your code here
}

The are a few cool things here. First one - the class will be automatically constructed in injected in your controller method by the IoC container, you don't need to do something special. The second cool thing is that the validation check is done before the Request object is passed to the controller, so if any validation error occurs you will be redirected back with all errors according to your rules set. This means that writing your code in the postCreateProfile method you can assume if this code get executed the validation is passed at this position and no additional check are needed by you.

I suggest you to migrate your code to use Laravel 5 Form Requests, because what you need is already implemented in the framework, and yes basically this is the point of the migration of one version to another. You can also check the documentation for more examples.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!