S
S
Sergey Nikolaev2015-07-08 20:26:10
Laravel
Sergey Nikolaev, 2015-07-08 20:26:10

Where do you store helper functions in Laravel?

There is the following

$validator = Validator::make($request->all(), [
            'page' => 'integer|required',
            'per_page'=> 'integer|required'
        ]);
        if($validator->fails()){
            return $validator->errors()->all();
        }

I would like to take it to an auxiliary class
class valid{
check($validate, $req){
$validator = Validator::make($req, $validate);
        if($validator->fails()){
            return $validator->errors()->all();
        }
}
}

and call from controller
$valid->check(['page' => 'integer|required','per_page'=> 'integer|required'], $request->all());

How this is done inside Laravel, where files are placed, etc.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
D', 2015-07-08
@Denormalization

For Laravel 5, it makes no sense to use a separate class for validation (as it was in L4).
In L5, there is a ValidatesRequests trait that can be used in controllers, and that's when the validate method comes in.
Better yet, use a custom FormRequest class to validate the data.
That is, we create the Http\Requests\UpdateUserProfile class and inherit it from Request.
In UpdateUserProfile we write 2 methods:

// Проверяем может ли пользователь делать это действие
public function authorize();
// Возвращаем массив с правилами
public function rules();

Then we use this class in the controller, let's say in the update method:
Validation will take place automatically, without unnecessary gestures, and if it passes, then $request contains 99.999% valid data.
Learn to read the documentation very carefully .

F
FMars, 2015-07-08
@FMars

Write in composer

"files": [
        "app/helpers.php"
    ]

Then:composer dumpautoload

A
Abc Edc, 2015-07-08
@gleber1

Well, you don't even have to enter it. The entire APP is automatically loaded by default!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question