Laravel 11 set default namespace for controllers

Updated: Feb 22, 2025

Laravel 11 set default namespace for controllers

To set a default namespace for controllers in Laravel, you can define a base path for your controller files and then use composer autoloading to load the controllers from that base path. Here are the steps to achieve this:

  1. Define a base path for your controllers:

Create a new directory for your controllers inside the app/Http/Controllers directory. Let's call it MyNamespace.

mkdir app/Http/Controllers/MyNamespace
  1. Define the __NAMESPACE__ constant in the new directory:

Create a new file called Controller.php inside the MyNamespace directory and define the __NAMESPACE__ constant at the top of the file.

<?php

namespace App\Http\Controllers\MyNamespace;

use Illuminate\Routing\Controller;

__NAMESPACE__;
  1. Create a service provider to register the new controller namespace:

Create a new service provider called App\Providers\ControllerServiceProvider.php. This provider will register the new controller namespace with Laravel.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Routing\Router;

class ControllerServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Boot the application routes file.
     *
     * @return void
     */
    public function boot(Router $router)
    {
        $this->app->bind('controller', function () {
            return new \App\Http\Controllers\Controller;
        });

        $this->app->bind('controller.namespace', function () {
            return __NAMESPACE__;
        });

        $this->app->singleton('controller', function ($app) {
            $controller = new \App\Http\Controllers\Controller;
            $controller->setAppPath($app->basePath());
            $controller->setNamespace($app['controller.namespace']);

            return $controller;
        });
    }
}
  1. Register the service provider:

Add the new service provider to the providers array in the config/app.php file.

'providers' => [
    // ...
    App\Providers\ControllerServiceProvider::class,
],
  1. Define the new controller namespace in the app/Providers/RouteServiceProvider.php file:

Update the map method in the App\Providers\RouteServiceProvider.php file to include the new controller namespace.

public function map()
{
    Route::middlewareGroup('web')
        ->namespace('App\Http\Controllers')
        ->group(base_path('routes/web.php'));

    Route::middlewareGroup('api')
        ->namespace('App\Http\Controllers')
        ->group(base_path('routes/api.php'));

    Route::middlewareGroup('admin')
        ->namespace('App\Http\Controllers\MyNamespace') // Add the new namespace here
        ->group(base_path('routes/admin.php'));
}
  1. Create a new controller:

Create a new controller file inside the MyNamespace directory and extend the Controller class.

<?php

namespace App\Http\Controllers\MyNamespace;

use App\Http\Controllers\Controller;

class HomeController extends Controller
{
    public function index()
    {
        return view('welcome');
    }
}

Now, you can use the new controller namespace in your routes file. For example:

Route::get('/', 'MyNamespace\HomeController@index');

And that's it! You have set a default namespace for controllers in Laravel using a custom base path and composer autoloading.