{"id":128620,"date":"2024-08-21T16:50:35","date_gmt":"2024-08-21T20:50:35","guid":{"rendered":"https:\/\/www.inmotionhosting.com\/support\/?p=128620"},"modified":"2024-08-21T16:56:46","modified_gmt":"2024-08-21T20:56:46","slug":"how-to-use-controllers-in-laravel","status":"publish","type":"post","link":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/","title":{"rendered":"How to Use Controllers in Laravel"},"content":{"rendered":"<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"538\" src=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/08\/use-controllers-in-laravel-1024x538.png\" alt=\"How to Use Controllers in Laravel\" class=\"wp-image-128622\" srcset=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/08\/use-controllers-in-laravel-1024x538.png 1024w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/08\/use-controllers-in-laravel-300x158.png 300w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/08\/use-controllers-in-laravel-768x403.png 768w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/08\/use-controllers-in-laravel.png 1200w\" sizes=\"auto, (min-width: 1360px) 876px, (min-width: 960px) calc(61.58vw + 51px), calc(100vw - 80px)\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/\">Laravel<\/a> is a powerful PHP framework that offers a clean and expressive syntax for web development. Controllers play a critical role in Laravel by handling the logic of your application, responding to user actions, and returning appropriate responses. In this guide, we\u2019ll explore how to effectively use controllers in Laravel, from creating them to using them in your <a href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-create-a-route-and-view-in-laravel\/\">routes<\/a>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#what\">What are Controllers in Laravel?<\/a><\/li>\n\n\n\n<li><a href=\"#creating\">Creating a Controller<\/a><\/li>\n\n\n\n<li><a href=\"#defining\">Defining Methods in a Controller<\/a><\/li>\n\n\n\n<li><a href=\"#routing\">Routing to Controller Methods<\/a><\/li>\n\n\n\n<li><a href=\"#resource-controller\">Resource Controllers<\/a><\/li>\n\n\n\n<li><a href=\"#middleware\">Middleware in Controllers<\/a><\/li>\n\n\n\n<li><a href=\"#dependency-injection\">Dependency Injection in Controllers<\/a><\/li>\n\n\n\n<li><a href=\"#forms\">Handling Form Requests<\/a><\/li>\n\n\n\n<li><a href=\"#returning\">Returning Responses<\/a><\/li>\n\n\n\n<li><a href=\"#api\">Using API Controllers<\/a><\/li>\n\n\n\n<li><a href=\"#best-practices\">Best Practices<\/a><\/li>\n\n\n\n<li><a href=\"#conclusion\">Conclusion<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"what\">What are Controllers in Laravel<\/h2>\n\n\n\n<p>Controllers in Laravel are classes that handle the logic behind incoming requests and provide the necessary responses. They are typically stored in the <code>app\/Http\/Controllers<\/code> directory. Controllers can group related request-handling logic into a single class, making your code more organized and easier to maintain.<\/p>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"creating\">Creating a Controller<\/h2>\n\n\n\n<p>Laravel provides a simple way to generate controllers using the Artisan command-line tool. To create a new controller, use the following command:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"php artisan make:controller MyController\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">php<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">artisan<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">make:controller<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">MyController<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This command creates a new file named <code>MyController.php<\/code> in the <code>app\/Http\/Controllers<\/code> directory. The new controller file will look something like this:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"&lt;?php\n\nnamespace App\\Http\\Controllers;\n\nuse Illuminate\\Http\\Request;\n\nclass MyController extends Controller\n{\n    \/\/\n}\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">&lt;?<\/span><span style=\"color: #79B8FF\">php<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">namespace<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">App\\Http\\Controllers<\/span><span style=\"color: #E1E4E8\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">use<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">Illuminate\\Http\\Request<\/span><span style=\"color: #E1E4E8\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">MyController<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">extends<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Controller<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #6A737D\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>You can now add methods to this class to handle various routes<\/p>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"defining\">Defining Methods in a Controller<\/h2>\n\n\n\n<p>Each method in a controller corresponds to a specific route or action in your application. For instance, if you want to handle a GET request to show a list of products, you could define a method like this:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"public function index()\n{\n    $products = Product::all();\n    return view('products.index', compact('products'));\n}\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">public<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">index<\/span><span style=\"color: #E1E4E8\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $products <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">Product<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">all<\/span><span style=\"color: #E1E4E8\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">view<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'products.index'<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">compact<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'products'<\/span><span style=\"color: #E1E4E8\">));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This <code>index<\/code> method fetches all products from the database and returns a view named <code>products.index<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"routing\">Routing to Controller Methods<\/h2>\n\n\n\n<p>To direct traffic to a specific controller method, you need to define it in your <code>routes\/web.php<\/code> file:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"use App\\Http\\Controllers\\MyController;\n\nRoute::get('\/products', [MyController::class, 'index']);\n\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">use<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">App\\Http\\Controllers\\MyController<\/span><span style=\"color: #E1E4E8\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #79B8FF\">Route<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">get<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'\/products'<\/span><span style=\"color: #E1E4E8\">, [<\/span><span style=\"color: #79B8FF\">MyController<\/span><span style=\"color: #F97583\">::class<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #9ECBFF\">'index'<\/span><span style=\"color: #E1E4E8\">]);<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This route definition means that when a user visits <code>\/products<\/code>, the <code>index<\/code> method of <code>MyController<\/code> will be executed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"resource-controller\">Resource Controllers<\/h2>\n\n\n\n<p>Laravel also offers a convenient way to handle CRUD (Create, Read, Update, Delete) operations through resource controllers. You can create a resource controller using the following command:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"php artisan make:controller ProductController --resource\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">php<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">artisan<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">make:controller<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">ProductController<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">--resource<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>A resource controller comes with predefined methods for the common CRUD actions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>index<\/code> \u2013 Show all items<\/li>\n\n\n\n<li><code>create<\/code> \u2013 Show form to create a new item<\/li>\n\n\n\n<li><code>store<\/code> \u2013 Handle the saving of a new item<\/li>\n\n\n\n<li><code>show<\/code> \u2013 Show a single item<\/li>\n\n\n\n<li><code>edit<\/code> \u2013 Show form to edit an existing item<\/li>\n\n\n\n<li><code>update<\/code> \u2013 Handle the updating of an item<\/li>\n\n\n\n<li><code>destroy<\/code> \u2013 Delete an item<\/li>\n<\/ul>\n\n\n\n<p>You can register all routes for these actions with a single line in your <code>routes\/web.php<\/code> file:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"Route::resource('products', ProductController::class);\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #79B8FF\">Route<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">resource<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'products'<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">ProductController<\/span><span style=\"color: #F97583\">::class<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This automatically creates routes for all CRUD operations associated with the <code>ProductController<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"middleware\">Middleware in Controllers<\/h2>\n\n\n\n<p>Middleware in Laravel provides a way to filter HTTP requests before they reach your controllers. You can assign middleware to a controller method or the entire controller:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"class MyController extends Controller\n{\n    public function __construct()\n    {\n        $this-&gt;middleware('auth');\n    }\n\n    public function index()\n    {\n        \/\/ This method is protected by the 'auth' middleware\n    }\n}\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">MyController<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">extends<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Controller<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">__construct<\/span><span style=\"color: #E1E4E8\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">middleware<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'auth'<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">index<\/span><span style=\"color: #E1E4E8\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #6A737D\">\/\/ This method is protected by the 'auth' middleware<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This ensures that only authenticated users can access the <code>index<\/code> method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"dependency-injection\">Dependency Injection in Controllers<\/h2>\n\n\n\n<p>Laravel\u2019s service container makes it easy to manage dependencies and inject them into your controller methods:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"public function show(ProductRepository $repository, $id)\n{\n    $product = $repository-&gt;find($id);\n    return view('products.show', compact('product'));\n}\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">public<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">show<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">ProductRepository<\/span><span style=\"color: #E1E4E8\"> $repository, $id)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $product <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> $repository<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">find<\/span><span style=\"color: #E1E4E8\">($id);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">view<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'products.show'<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">compact<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'product'<\/span><span style=\"color: #E1E4E8\">));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In this example, <code>ProductRepository<\/code> is automatically resolved by the service container and injected into the <code>show<\/code> method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"forms\">Handling Form Requests<\/h2>\n\n\n\n<p>Laravel also allows you to validate and process form requests using custom <code>FormRequest<\/code> classes. You can create a form request class using Artisan:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"php artisan make:request StoreProductRequest\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">php<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">artisan<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">make:request<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">StoreProductRequest<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This class can be used in a controller method to handle validation:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"use App\\Http\\Requests\\StoreProductRequest;\n\npublic function store(StoreProductRequest $request)\n{\n    \/\/ Validation is already done by StoreProductRequest\n    $validated = $request-&gt;validated();\n\n    Product::create($validated);\n\n    return redirect()-&gt;route('products.index');\n}\n\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">use<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">App\\Http\\Requests\\StoreProductRequest<\/span><span style=\"color: #E1E4E8\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">public<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">store<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">StoreProductRequest<\/span><span style=\"color: #E1E4E8\"> $request)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #6A737D\">\/\/ Validation is already done by StoreProductRequest<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $validated <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> $request<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">validated<\/span><span style=\"color: #E1E4E8\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #79B8FF\">Product<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">create<\/span><span style=\"color: #E1E4E8\">($validated);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">redirect<\/span><span style=\"color: #E1E4E8\">()<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">route<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'products.index'<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"returning\">Returning Responses<\/h2>\n\n\n\n<p>Laravel controllers allow you to return various types of responses:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Views:<\/strong> Render a Blade template using the <code>view<\/code> helper.<\/li>\n\n\n\n<li><strong>JSON:<\/strong> Return a JSON response using the <code>response()-&gt;json()<\/code> method.<\/li>\n\n\n\n<li><strong>Redirects:<\/strong> Redirect users to a different page using <code>redirect()-&gt;route()<\/code> or <code>redirect()-&gt;back()<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"public function store(StoreProductRequest $request)\n{\n    \/\/ Save the product\n    Product::create($request-&gt;validated());\n\n    \/\/ Return a JSON response\n    return response()-&gt;json(['message' =&gt; 'Product created successfully']);\n}\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">public<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">store<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">StoreProductRequest<\/span><span style=\"color: #E1E4E8\"> $request)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #6A737D\">\/\/ Save the product<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #79B8FF\">Product<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">create<\/span><span style=\"color: #E1E4E8\">($request<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">validated<\/span><span style=\"color: #E1E4E8\">());<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #6A737D\">\/\/ Return a JSON response<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">response<\/span><span style=\"color: #E1E4E8\">()<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">json<\/span><span style=\"color: #E1E4E8\">([<\/span><span style=\"color: #9ECBFF\">'message'<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">=&gt;<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">'Product created successfully'<\/span><span style=\"color: #E1E4E8\">]);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"api\">Using API Controllers<\/h2>\n\n\n\n<p>Laravel provides a simple way to build API controllers using the <code>apiResource<\/code> method, which is similar to <code>resource<\/code> but optimized for APIs:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"Route::apiResource('products', ProductController::class);\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #79B8FF\">Route<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">apiResource<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'products'<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">ProductController<\/span><span style=\"color: #F97583\">::class<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>API controllers don\u2019t include routes for <code>create<\/code> or <code>edit<\/code>, as these operations typically involve forms and are irrelevant in API contexts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"best-practices\">Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Single Responsibility:<\/strong> Ensure each controller method has a single responsibility, making your code easier to maintain.<\/li>\n\n\n\n<li><strong>RESTful Structure:<\/strong> Follow RESTful conventions for method names and routes.<\/li>\n\n\n\n<li><strong>Use Resource Controllers:<\/strong> Use resource controllers to minimize repetitive code for CRUD operations.<\/li>\n\n\n\n<li><strong>Validation:<\/strong> Leverage form requests for validation to keep controller methods clean.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>Controllers in <a href=\"https:\/\/laravel.com\">Laravel<\/a> are potent tools that help organize your application\u2019s logic. By following best practices and utilizing Laravel\u2019s built-in features, such as resource controllers and middleware, you can easily create robust, maintainable applications. Whether handling web requests or building APIs, Laravel controllers are vital in delivering a seamless user experience.<\/p>\n\n\n<div class=\"jumbotron\">\r\n<p>Boost your Laravel apps with our specialized <a href=\"https:\/\/www.inmotionhosting.com\/laravel-hosting\">Laravel Hosting<\/a>. Experience faster speeds for your Laravel applications and websites thanks to NVMe storage, server protection, dedicated resources, and optimization tools.<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/design.inmotionhosting.com\/assets\/icons\/standard\/check-blue.svg\" alt=\"check mark\" width=\"24\" height=\"24\" \/>99.99% Uptime    <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/design.inmotionhosting.com\/assets\/icons\/standard\/check-blue.svg\" alt=\"check mark\" width=\"24\" height=\"24\" \/>Free SSL    <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/design.inmotionhosting.com\/assets\/icons\/standard\/check-blue.svg\" alt=\"check mark\" width=\"24\" height=\"24\" \/>Dedicated IP Address    <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/design.inmotionhosting.com\/assets\/icons\/standard\/check-blue.svg\" alt=\"check mark\" width=\"24\" height=\"24\" \/>Developer Tools<\/p>\r\n<p><a class=\"btn btn-primary btn-lg\" href=\"https:\/\/www.inmotionhosting.com\/laravel-hosting\">Laravel Hosting<\/a><\/p>\r\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Laravel is a powerful PHP framework that offers a clean and expressive syntax for web development. Controllers play a critical role in Laravel by handling the logic of your application, responding to user actions, and returning appropriate responses. In this guide, we&#8217;ll explore how to effectively use controllers in Laravel, from creating them to using<a class=\"moretag\" href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/\"> Read More ><\/a><\/p>\n","protected":false},"author":57032,"featured_media":128622,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4486],"tags":[],"class_list":["post-128620","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Use Controllers in Laravel | InMotion Hosting<\/title>\n<meta name=\"description\" content=\"Learn how to effectively use Laravel controllers to handle application logic, manage routes, and create clean, maintainable code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use Controllers in Laravel | InMotion Hosting\" \/>\n<meta property=\"og:description\" content=\"Learn how to effectively use Laravel controllers to handle application logic, manage routes, and create clean, maintainable code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/\" \/>\n<meta property=\"og:site_name\" content=\"InMotion Hosting Support Center\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/inmotionhosting\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-08-21T20:50:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-08-21T20:56:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/08\/use-controllers-in-laravel.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Derrell\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:site\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Derrell\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/\"},\"author\":{\"name\":\"Derrell\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/0736f70b4077032374f89709cdc255b7\"},\"headline\":\"How to Use Controllers in Laravel\",\"datePublished\":\"2024-08-21T20:50:35+00:00\",\"dateModified\":\"2024-08-21T20:56:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/\"},\"wordCount\":713,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/08\/use-controllers-in-laravel.png\",\"articleSection\":[\"Laravel\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/\",\"name\":\"How to Use Controllers in Laravel | InMotion Hosting\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/08\/use-controllers-in-laravel.png\",\"datePublished\":\"2024-08-21T20:50:35+00:00\",\"dateModified\":\"2024-08-21T20:56:46+00:00\",\"description\":\"Learn how to effectively use Laravel controllers to handle application logic, manage routes, and create clean, maintainable code.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/#primaryimage\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/08\/use-controllers-in-laravel.png\",\"contentUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/08\/use-controllers-in-laravel.png\",\"width\":1200,\"height\":630},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.inmotionhosting.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Use Controllers in Laravel\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"name\":\"InMotion Hosting Support Center\",\"description\":\"Web Hosting Support &amp; Tutorials\",\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\",\"name\":\"InMotion Hosting\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"contentUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"width\":696,\"height\":696,\"caption\":\"InMotion Hosting\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/inmotionhosting\/\",\"https:\/\/x.com\/InMotionHosting\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/0736f70b4077032374f89709cdc255b7\",\"name\":\"Derrell\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/derrell-willis\"],\"url\":\"https:\/\/www.inmotionhosting.com\/support\/author\/derrellw\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Use Controllers in Laravel | InMotion Hosting","description":"Learn how to effectively use Laravel controllers to handle application logic, manage routes, and create clean, maintainable code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/","og_locale":"en_US","og_type":"article","og_title":"How to Use Controllers in Laravel | InMotion Hosting","og_description":"Learn how to effectively use Laravel controllers to handle application logic, manage routes, and create clean, maintainable code.","og_url":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/","og_site_name":"InMotion Hosting Support Center","article_publisher":"https:\/\/www.facebook.com\/inmotionhosting\/","article_published_time":"2024-08-21T20:50:35+00:00","article_modified_time":"2024-08-21T20:56:46+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/08\/use-controllers-in-laravel.png","type":"image\/png"}],"author":"Derrell","twitter_card":"summary_large_image","twitter_creator":"@InMotionHosting","twitter_site":"@InMotionHosting","twitter_misc":{"Written by":"Derrell","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/#article","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/"},"author":{"name":"Derrell","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/0736f70b4077032374f89709cdc255b7"},"headline":"How to Use Controllers in Laravel","datePublished":"2024-08-21T20:50:35+00:00","dateModified":"2024-08-21T20:56:46+00:00","mainEntityOfPage":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/"},"wordCount":713,"commentCount":0,"publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/#primaryimage"},"thumbnailUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/08\/use-controllers-in-laravel.png","articleSection":["Laravel"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/","url":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/","name":"How to Use Controllers in Laravel | InMotion Hosting","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/#primaryimage"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/#primaryimage"},"thumbnailUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/08\/use-controllers-in-laravel.png","datePublished":"2024-08-21T20:50:35+00:00","dateModified":"2024-08-21T20:56:46+00:00","description":"Learn how to effectively use Laravel controllers to handle application logic, manage routes, and create clean, maintainable code.","breadcrumb":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/#primaryimage","url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/08\/use-controllers-in-laravel.png","contentUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/08\/use-controllers-in-laravel.png","width":1200,"height":630},{"@type":"BreadcrumbList","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-use-controllers-in-laravel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.inmotionhosting.com\/support\/"},{"@type":"ListItem","position":2,"name":"How to Use Controllers in Laravel"}]},{"@type":"WebSite","@id":"https:\/\/www.inmotionhosting.com\/support\/#website","url":"https:\/\/www.inmotionhosting.com\/support\/","name":"InMotion Hosting Support Center","description":"Web Hosting Support &amp; Tutorials","publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.inmotionhosting.com\/support\/#organization","name":"InMotion Hosting","url":"https:\/\/www.inmotionhosting.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","contentUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","width":696,"height":696,"caption":"InMotion Hosting"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/inmotionhosting\/","https:\/\/x.com\/InMotionHosting"]},{"@type":"Person","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/0736f70b4077032374f89709cdc255b7","name":"Derrell","sameAs":["https:\/\/www.linkedin.com\/in\/derrell-willis"],"url":"https:\/\/www.inmotionhosting.com\/support\/author\/derrellw\/"}]}},"jetpack_featured_media_url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/08\/use-controllers-in-laravel.png","jetpack_sharing_enabled":true,"primary_category":{"id":4486,"name":"Laravel","slug":"laravel","link":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/"},"_links":{"self":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/128620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/users\/57032"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/comments?post=128620"}],"version-history":[{"count":4,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/128620\/revisions"}],"predecessor-version":[{"id":129503,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/128620\/revisions\/129503"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/media\/128622"}],"wp:attachment":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/media?parent=128620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/categories?post=128620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/tags?post=128620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}