EDDYMENS

Laravel: Create Controllers In A Subfolder (Sub Controller)


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 😊 "Laravel file driven cache on steroids"