How to use middleware in Laravel?

Somehow got the opinion, on the basis of the scanned video material and examples from the Internet that middleware is used only to check authentication and authorization of users.
Is there, for example, it makes sense to use middlaware to check the validity of the data transmitted from the controller?
Please help me to understand.
March 19th 20 at 09:27
4 answers
March 19th 20 at 09:29
Solution
Validation of form fields
Create the form.
Create a Request file.
It prescribes rules and messages.
Plug in the controller, in a method where there is a post request and the file to validate field.
Middleware has validated field. Used to filter queries. For example, to allow access to certain of ranting only to users who meet the conditions. Plus carry out steps in the verification process. For example, blocked razlozhenii and redirect on the main.
March 19th 20 at 09:31
No. XS in larval, but if it's mvc, then the data request obratyatsya in the controller, and middlaware there will be the front of your processing in the controller. That is, for example authorization, we middlaware check whether the user is authorized, and if so, then pass the data on to the processing in the controller. In practice, they it makes sense to use if for example the analyst came and said that if we process a vegetable, then it should display a message to a person: you are a tomato and you have a bunch of controller: kartoffelkeller, kapustianyi, lucknroll, etc., and you instead of them all to insert a message you tomato, add middlaware and it look vegetable it is, and if so, display the message you tomato.
March 19th 20 at 09:33
For validation of fields that have a validator

public function store(Request $request, StoreBlogTag $store)
{
$validate = Validator::make($request->all(), $store->rules(), $store->messages());
if ($validate->fails()) {
return redirect('post/create')
->withErrors($validate)
->withInput();
};
$tag = Tag::firstOrCreate([
'title' => $request->input('title'),
'slug' => Str::slug($request->input('title'), '-'),
]);
$tag->save();
return redirect()->back();
}
Tin - evangeline.Smith commented on March 19th 20 at 09:36
There Are FormRequest - Gerson commented on March 19th 20 at 09:39
@evangeline.Smith, explain? - Alvera_Walsh commented on March 19th 20 at 09:42
Let's start with the fact that for some reason, you bring a piece of torn unformatted code in which a bunch of obscure objects and approaches. It's just disrespectful to the community. OK, we are not stupid, will understand.

1.
public function store(Request $request, StoreBlogTag $store)

Well, we kind of realized that StoreBlogTag stored validation rules and errors for validation for tags. Why all this is necessary from the current code does not understand. As already noted, all this can be replaced by a Form Request Validation.

2.
$validate = Validator::make($request->all(), $store->rules(), $store->messages());
if ($validate->fails()) {
 return redirect('post/create')
->withErrors($validate)
->withInput();
};

Why do we have this kopipasta? You kind of create tags. Why the redirects to `post/create` - how can this be understood? What it generally gives? Here are some specific redirect? Otherwise, you can lay it all on Larval and replace with:
Validator::make($request->all(), $store->rules(), $store->messages())->validate();

Although, again not understand, why bother to do the validation using make?

3.
$tag = Tag::firstOrCreate([
'title' => $request->input('title'),
'slug' => Str::slug($request->input('title'), '-'),
]);

Why bother in this case to bring such a complex example? It is impossible that any simple create with mass-assignment to lead? Why make a check on two fields? Why slug is generated in the controller? For slug actually full of ready-made solutions in the form of traits.

4.
$tag->save();
It's like, if suddenly, for the first time is not preserved (firstOrCreate method creates an entry in the DB), again save for podstrahovat :)

In short, here is an example for the default (return resources because it is API):
class Controller extends BrandController
{
 // [...]
 public function store(SaveBrand $request)
{
 $brand = Brand::create($request->all());
 return new BrandResource($brand);
}

 public function update(SaveBrand $request, Brand $brand)
{
$brand->update($request->all());
 return new BrandResource($brand);
}
}

+
SaveBrand class extends FormRequest
{
 public function authorize()
{
 return true;
}

 public function rules()
{
 $rules = [
 'name' => [
'required',
'max:100',
Rule::unique('brands')->ignore($this->id)
],
 'url' => [
'required',
'max:100',
'url',
Rule::unique('brands')->ignore($this->id)
]
];

 // Allows you to edit one field in the query
 if ($this->method() == 'PATCH') {
 foreach ($rules as $key => $item) {
 array_unshift($rules[$key], 'sometimes');
}
}

 return $rules;
}
}

+
class BrandResource extends JsonResource
{
 public function toArray($request)
{
 return parent::toArray($request);
}
}
- evangeline.Smith commented on March 19th 20 at 09:45
@evangeline.Smith, thanks for the reply. It really is a piece that redirect to the post. Sooobschenie not formatted, inserted from the phone. Well thanks anyway for feedback. - Alvera_Walsh commented on March 19th 20 at 09:48
March 19th 20 at 09:35
Don't try to fan controllers, they perform their direct functions, write logic in service classes, validated write to requested, mediators nemozno be used not only for authorization, according to best practice in laravale, they are used to work with incoming http requests, check the tokens, add and check special headers, redirections, logging, cors, etc. etc, i.e. all the application logic which should work up to performing the business logic

Find more questions by tags Laravel