{"id":11050,"date":"2021-11-25T10:00:00","date_gmt":"2021-11-25T01:00:00","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=11050"},"modified":"2021-11-24T19:38:31","modified_gmt":"2021-11-24T10:38:31","slug":"cleaning-up-laravel-controllers","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/11050","title":{"rendered":"Cleaning Up Laravel Controllers"},"content":{"rendered":"\n<p> <strong>Introduction<\/strong> <\/p>\n\n\n\n<p>Controllers play a huge role in any&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.guru99.com\/mvc-tutorial.html\" data-type=\"URL\" data-id=\"https:\/\/www.guru99.com\/mvc-tutorial.html\" target=\"_blank\">MVC<\/a> (model view controller) based project. They\u2019re effectively the \u201cglue\u201d that takes a user\u2019s request, performs some type of logic, and then returns a response. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p> <strong>The Problem with Bloated Controllers<\/strong> <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Bloated controllers can cause several problems for developers. They can:<\/p>\n\n\n\n<p> 1.  Make it hard to track down a particular piece of code or functionality.&nbsp;<\/p>\n\n\n\n<p> 2.  Make it difficult to spot the exact location of a bug.&nbsp;<\/p>\n\n\n\n<p> 3. Make it harder to write tests for more complex requests.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>T<strong>he Bloated Controller<\/strong> <\/p>\n\n\n\n<p> For this blog, I am going to use an example&nbsp;<code>UserController<\/code>: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class UserController extends Controller\n{\n    public function store(Request $request): RedirectResponse\n    {\n        $this-&gt;authorize('create', User::class);        $request-&gt;validate([\n            'name'     =&gt; 'string|required|max:50',\n            'email'    =&gt; 'email|required|unique:users',\n            'password' =&gt; 'string|required|confirmed',\n        ]);        $user = User::create([\n            'name'     =&gt; $request-&gt;name,\n            'email'    =&gt; $request-&gt;email,\n            'password' =&gt; $request-&gt;password,\n        ]);        $user-&gt;generateAvatar();\n        $this-&gt;dispatch(RegisterUserToNewsletter::class);        return redirect(route('users.index'));\n    }    public function unsubscribe(User $user): RedirectResponse\n    {\n        $user-&gt;unsubscribeFromNewsletter();        return redirect(route('users.index'));\n    }\n}\n\n<strong>1. Lift Validation and Authorization into Form Requests\n\n<\/strong>\n\nOne of the first things that we can do with the controller is to lift any validation and authorization out of the controller and into a form request class.\n\nWe\u2019ll use the following Artisan command to create a new form request:\n\n<strong>php artisan make:request StoreUserRequest<\/strong>\n\nThe above command will have created a new app\/Http\/Requests\/StoreUserRequest.php class that looks like this:\n\nclass StoreUserRequest extends FormRequest\n{\n    \/**\n     * Determine if the user is authorized to make this request.\n     *\n     * @return bool\n     *\/\n    public function authorize()\n    {\n        return false;\n    }\n    \/**\n     * Get the validation rules that apply to the request.\n     *\n     * @return array\n     *\/\n    public function rules()\n    {\n        return [\n            \/\/\n        ];\n    }\n}\n\n\nWe can use the authorize() method to determine if the user should be allowed to carry out the request. The method should return true if they can and false if they cannot. We can also use the rules() method to specify any validation rules that should be run on the request body. \n\nclass StoreUserRequest extends FormRequest\n{\n    \/**\n     * Determine if the user is authorized to make this request.\n     *\n     * @return bool\n     *\/\n    public function authorize(): bool\n    {\n        return Gate::allows('create', User::class);\n    }\n    \/**\n     * Get the validation rules that apply to the request.\n     *\n     * @return array\n     *\/\n    public function rules(): array\n    {\n        return [\n            'name'     =&gt; 'string|required|max:50',\n            'email'    =&gt; 'email|required|unique:users',\n            'password' =&gt; 'string|required|confirmed',\n        ];\n    }\n}\n\nOur controller should now also look like this:\n\nclass UserController extends Controller\n{\n    public function store(StoreUserRequest $request): RedirectResponse\n    {\n        $user = User::create([\n            'name'     =&gt; $request-&gt;name,\n            'email'    =&gt; $request-&gt;email,\n            'password' =&gt; $request-&gt;password,\n        ]);\n        $user-&gt;generateAvatar();\n        $this-&gt;dispatch(RegisterUserToNewsletter::class);\n        return redirect(route('users.index'));\n    }\n    public function unsubscribe(User $user): RedirectResponse\n    {\n        $user-&gt;unsubscribeFromNewsletter();\n        return redirect(route('users.index'));\n    }\n}\n\n<strong>2. Move Common Logic into Actions or Services<\/strong>\n\nAnother step that we could take to clean up the store() method could be to move out our \"business logic\" into a separate action or service class.\n\nclass StoreUserAction\n{\n    public function execute(Request $request): void\n    {\n        $user = User::create([\n            'name'     =&gt; $request-&gt;name,\n            'email'    =&gt; $request-&gt;email,\n            'password' =&gt; $request-&gt;password,\n        ]);\n        $user-&gt;generateAvatar();\n        $this-&gt;dispatch(RegisterUserToNewsletter::class);\n    }\n}\n\nNow we can update our controller to use the action:\n\nclass UserController extends Controller\n{\n    public function store(StoreUserRequest $request, StoreUserAction $storeUserAction): RedirectResponse\n    {\n        $storeUserAction-&gt;execute($request);\n        return redirect(route('users.index'));\n    }\n    public function unsubscribe(User $user): RedirectResponse\n    {\n        $user-&gt;unsubscribeFromNewsletter();\n        return redirect(route('users.index'));\n    }\n}\n\n<strong>3. Use Resource or Single-use Controllers<\/strong>\n\nA great way of keeping controllers clean is to ensure that they are either \u201c <a rel=\"noreferrer noopener\" href=\"https:\/\/laravel.com\/docs\/8.x\/controllers#resource-controllers\" data-type=\"URL\" data-id=\"https:\/\/laravel.com\/docs\/8.x\/controllers#resource-controllers\" target=\"_blank\">resource controllers<\/a>\u201d or \u201c <a rel=\"noreferrer noopener\" href=\"https:\/\/laravel.com\/docs\/8.x\/controllers#single-action-controllers\" data-type=\"URL\" data-id=\"https:\/\/laravel.com\/docs\/8.x\/controllers#single-action-controllers\" target=\"_blank\">single-use controllers\u201d. <\/a>\n\nA resource controller is a controller that provides functionality based around a particular resource. So, in our case, our resource is the User model and we want to be able to perform all CRUD (create, update, update, delete) operations on this model. A resource controller typically contains index(), create(), store(), show(), edit(), update() and destroy() methods.\n\nA single-use controller is a controller that only has one public __invoke() method. These are really useful if you have a controller that doesn't really fit into one of the RESTful methods that we have in our resource controllers.\n\nSo let\u2019s create a new controller using the following Artisan command:\n\n\n<strong>php artisan make:controller UnsubscribeUserController -i<\/strong>\n\nNotice how we passed -i to the command so that the new controller will be an invokable, single-use controller. We should now have a controller that looks like this:\n\nclass UnsubscribeUserController extends Controller\n{\n    public function __invoke(Request $request)\n    {\n        \/\/\n    }\n}\n\nWe can now move our method\u2019s code over and delete the unsubscribe method from our old controller:\n\nclass UnsubscribeUserController extends Controller\n{\n    public function __invoke(Request $request): RedirectResponse\n    {\n        $user-&gt;unsubscribeFromNewsletter();\n        return redirect(route('users.index'));\n    }\n}\n\n\n<strong>Conclusion<\/strong>\n\nHopefully this article has given you an insight into the different types of things you can do to clean up your controllers in your Laravel projects.\n\n\nTsuki<\/pre>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11050\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/11050\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Cleaning Up Laravel Controllers\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11050\" data-text=\"Cleaning Up Laravel Controllers\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11050\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11050\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Controllers play a huge role in any&nbsp;MVC (model view controller) based project. They\u2019re effec [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[35],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11050"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=11050"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11050\/revisions"}],"predecessor-version":[{"id":11053,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11050\/revisions\/11053"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=11050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=11050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=11050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}