{"flag":true,"single":true,"pageTitle":"create controller in subfolder","post":{"id":125,"user_id":"1","slug":"create-controller-in-subfolder-vtuw","title":"create controller in subfolder","body":"<p>In Laravel, controllers are used to handle <strong>incoming HTTP requests<\/strong> and perform actions based on those requests. By <strong>default<\/strong>, Laravel creates controllers in the<strong> \"app\/Http\/Controllers\"<\/strong> directory, but it is also possible to organize controllers into subfolders for better organization.<\/p>\r\n<p>To create a controller in a subfolder, follow these steps:<\/p>\r\n<p>1.<strong>Create a new folder<\/strong> inside the \"app\/Http\/Controllers\" directory. For example, you can create a folder called <strong>\"Admin\".<\/strong><\/p>\r\n<p>2.<strong> Create a new controller<\/strong> inside the subfolder you just created. For example, you can create a controller called \"AdminController\" inside the \"Admin\" folder. To create a controller, you can use the <strong>\"php artisan make:controller\"<\/strong> command in the terminal:<\/p>\r\n<pre class=\"language-markup\"><code>php artisan make:controller Admin\/AdminController<\/code><\/pre>\r\n<p>3. In the new controller, <strong>define the methods <\/strong>that will handle incoming HTTP requests. For example, you can define a method called \"<strong>testo<\/strong>\" that will handle GET requests to the<strong> \"\/admin\/testo\" <\/strong>URL:<\/p>\r\n<pre class=\"language-markup\"><code>&lt;?php\r\nnamespace App\\Http\\Controllers\\Admin;\r\nuse App\\Http\\Controllers\\Controller;\r\nclass AdminController extends Controller\r\n{\r\n    public function testo()\r\n    {\r\n        return 'This is a test controller in a subfolder!';\r\n    }\r\n}\r\n<\/code><\/pre>\r\n<p>4. In your routes file, use the \"Route::controller\" method to define routes that will be handled by the controller. In the example below, we're using the \"Route::controller\" method to map all HTTP methods to the \"AdminController\" controller in the \"Admin\" subfolder.&nbsp;<\/p>\r\n<pre class=\"language-markup\"><code>Route::controller(\\App\\Http\\Controllers\\Admin\\AdminController::class)-&gt;group( function () {\r\n    Route::get('\/admin\/testo', 'testo');\r\n});<\/code><\/pre>\r\n<p>You can now access the \"<strong>testo<\/strong>\" method in the \"<strong>AdminController<\/strong>\" controller by visiting the <strong>\"\/admin\/testo\"<\/strong> URL in your browser.<\/p>\r\n<p>That's it! You've successfully created a controller in a subfolder and defined routes that are handled by that controller. By organizing your controllers into subfolders, you can make your codebase easier to navigate and maintain.<\/p>","category_id":"2","is_private":"0","created_at":"2023-04-27T06:17:46.000000Z","updated_at":"2023-04-27T06:17:46.000000Z","category":{"id":2,"user_id":"1","name":"Laravel Core","slug":"laravel-nhyt","parent_id":"1","created_at":"2023-03-14T03:58:27.000000Z","updated_at":"2023-03-20T11:30:50.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"In Laravel, controllers are used to handle incoming HTTP requests and perform actions based on those requests. By default, Laravel creates c - create controller in subfolder (Updated: April 27, 2023) - Read more about create controller in subfolder at my programming site [SITE]","categories":[]}