DEV Community

Cover image for Custom Namespaces to Organize Your Laravel Controllers
Bobby Iliev
Bobby Iliev

Posted on • Originally published at devdojo.com

Custom Namespaces to Organize Your Laravel Controllers

Introduction

In some cases, when you have too many Controllers in your Laravel project, your app/Http/Controllers directory might get cluttered with too many Controllers.

To keep things more organized, you could use custom Laravel namespaces to store your Controllers in different directories inside the app/Http/Controllers directory.

In this tutorial, you will learn how to use a custom namespace for your Laravel Controllers!

Prerequisites

Before you start, you would need to have a Laravel application up and running.

I will be using a DigitalOcean Ubuntu Droplet for this demo. If you wish, you can use my affiliate code to get free $100 DigitalOcean credit to spin up your own servers!

If you do not have that yet, you can follow the steps from this tutorial on how to do that:

Or you could use this awesome script to do the installation:

Creating a standard controller

By default, to create a Controller in Laravel, you would run the following artisan command:

php artisan make:controller DemoController --resource
Enter fullscreen mode Exit fullscreen mode

Note: in case that you are not familiar with the --resource flag, all that it does is to create a controller with all methods needed for your CRUD.

By default, this would create your controller inside the app/Http/Controllers/ directory.

Now that we've covered that let's learn how to create a controller in a custom namespace!

Creating a Controller in a custom namespace

In order to create a controller in a custom namespace, you would need to use the following command:

php artisan make:controller Demo/DemoController --resource
Enter fullscreen mode Exit fullscreen mode

This will create your DemoController inside a Demo directory:

app/Http/Controllers/Demo/DemoController.php
Enter fullscreen mode Exit fullscreen mode

Note that we use a slash / to separate our namespace with our controller.

With that, we are actually creating a namespace as well.

This is quite handy as you don't have to stack all of your Controllers directly inside the app/Http/Controllers/ directory but instead organize them in a more suitable for you way.

Let's edit the file and update the index method with a simple return statement. With your text editor of choice, open the app/Http/Controllers/Demo/DemoController.php and update your index method as follows:

    public function index()
    {
        return "Custom Namespaces are awesome!";
    }
Enter fullscreen mode Exit fullscreen mode

Make sure to save the file after the change.

Now that we have our custom namespace in place, let's learn how to set up our routes!

Routing to the custom namespace

In order to define a route for the custom namespace that we've just created, open the routes/web.php file and add the following:

Route::namespace('Demo')->group(function() {

});
Enter fullscreen mode Exit fullscreen mode

Thanks to the namespace method, we can specify the new custom Demo namespace.

After that, you can add your routes as normal.

Route::namespace('Demo')->group(function() {
    Route::resource('demo', DemoController::class);
});
Enter fullscreen mode Exit fullscreen mode

For more information on Laravel Routes, make sure to check out the documentation here.

Conclusion

Now you know how to create a Controller in a custom namespace and keep your controllers well organized.

For more information on Laravel controllers, make sure to check out the official docs here:

Laravel Controllers Documentaiton

If you are just getting started with Laravel, make sure to check out this introduction course here:

Getting started with Laravel

Top comments (1)

Collapse
 
insan3 profile image
Oscar Nevárez

Great content, Bobby. thanks for sharing.