EDDYMENS

Last updated 2023-03-13 07:11:18

Laravel: Create Controllers In A Subfolder (Sub Controller)

Table of contents

01: . 02: β”œβ”€β”€ Controllers 03: β”‚ β”œβ”€β”€ Map 04: β”‚ β”‚ └── MainController.php 05: β”‚ β”œβ”€β”€ Blog 06: β”‚ β”œβ”€β”€ Book 07: β”‚ └── Controller.php

Creating the subfolder

Placing your controller in a subfolder starts by actually placing the controller in the folder. The folder structure above shows the controller MainController placed in the Map folder.

Updating the namespace

Once you are done with that the next thing to do is update the namespace to reflect this change. Thus:

01: <?php 02: 03: namespace App\Http\Controllers\Map;

Updating references

Next, since your controller extends the base controller i.e: Controller.php we might need to update the reference to that as well. If you use anything from Laravel 9 you will certainly need to update this reference.

01: <?php 02: 03: namespace App\Http\Controllers\Map; 04: 05: use Illuminate\Http\Request; 06: use App\Http\Controllers\Controller as ParentController; 07: 08: class MainController extends ParentController 09: { 10: ...

On line 06 we explicitly import the base controller and alias it as ParentController, we then extend our controller using the alias on line 08.

Here is another article you might like 😊 "Diary Of Insights: A Documentation Of My Discoveries"