mercredi 23 janvier 2019

What is better way to validating incoming request out of controller?

The standard way to validating incoming requests in Laravel is something like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PostController extends Controller
{
    /**
     * Show the form to create a new blog post.
     *
     * @return Response
     */
    public function create()
    {
        return view('post.create');
    }

    /**
     * Store a new blog post.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        $validatedData = $request->validate([
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ]);

        // The blog post is valid...
    }
}

There’s nothing wrong with validating requests in controllers, But how could I write the validation logic out of the controller to keep it clean and not break Single Responsibility Principle?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire