Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vcgnetwork's avatar

Laravel 11 New Install says controllers are not found.

I want to setup a default namespace. I don't want extra text in my route web.php file

example: Route::get('/', 'PageController@welcome'); -VS- Route::get('/', 'App\Http\Controllers\PageController@welcome');

protected $namespace = 'App\Http\Controllers';

I am not sure where to put this in the new structure.

0 likes
22 replies
LaryAI's avatar
Level 58

In Laravel 11, if you want to set a default namespace for your routes so that you don't have to specify the full namespace each time you define a route, you can do so in the RouteServiceProvider class. Here's how you can set it up:

  1. Open the RouteServiceProvider.php file located in the app/Providers directory.

  2. In the RouteServiceProvider class, you will find a boot method. You can set the default namespace for your routes within this method by using the ->namespace() method on the Route facade.

Here's an example of how to set the default namespace:

<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    // ...

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        // ...

        // Set the default namespace for the routes.
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));

        // ...
    }

    // ...
}

With this setup, you can now define your routes in routes/web.php without needing to specify the full namespace each time:

<?php

use Illuminate\Support\Facades\Route;

// You can now use the controller name directly without the full namespace.
Route::get('/', 'PageController@welcome');

This tells Laravel to prefix the namespace App\Http\Controllers to your controller classes when resolving them in your routes, allowing you to reference them with their short names as shown above.

vcgnetwork's avatar

Someone please help I followed the directions above and its still giving me the error about: "Target class [PageController] does not exist."

Already ran composer dumpautoload

jlrdw's avatar

Give them a namespace.

1 like
jlrdw's avatar

@vcgnetwork Just like your example above in the routes file. That's what I do to avoid all of the use statements.

vcgnetwork's avatar

@jlrdw something else is missing in the directions above. It still says Controller is missing

jlrdw's avatar

@vcgnetwork

Just do this in web.php

$np = 'App\Http\Controllers\';

Double backslashes, they didn't show here.

$np = 'App\\Http\\Controllers\';

then

Route::get('dog/index', $np . 'DogController@index');
gych's avatar

@jlrdw Its possible to show double backslashes in code block but you'll have to add 3 instead of two

$np = 'App\\Http\\Controllers\';
MichalOravec's avatar

Most likely setRootControllerNamespace will work, so add it to your AppServiceContainer.

use Illuminate\Support\Facades\URL;

public function boot(): void
{
    URL::setRootControllerNamespace('App\Http\Controllers');
}
vcgnetwork's avatar

@michaloravec Sorry, still same result.

This was a new install and I just wanted to set a namespace. I'm sure this is a quirk that get fixed eventually.

MichalOravec's avatar

@vcgnetwork I was pretty sure that it will work. Unfortunately I don't have set up my environment for Laravel 11, so I can't test it at the moment.

1 like
jlrdw's avatar

@vcgnetwork using:

$np = 'App\\Http\\Controllers\\';

Didn't work? It works in my laravel 11 new install.

1 like
puklipo's avatar

If it's a new project, update your knowledge to how to use the new routing in Laravel 8 and later. namespace is used for compatibility with older versions.

use App\Http\Controllers\PageController;

Route::get('/', [PageController::class, 'welcome']);

// People who want to write more simply use the __invoke() controller
Route::get('/', WelcomeController::class);
// Use resource controller except for __invoke controller.
Route::resource('post', PostController::class);
// If only view
Route::view('welcome', 'welcome');

If you really don't like it https://laracasts.com/discuss/channels/laravel/laravel-11-1

1 like
jlrdw's avatar
jlrdw
Best Answer
Level 75

@puklipo good one, the namespace part isn't documented, but it works. In ver 10 it was in RouteServiceProvider:

<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{

    /**
     * The path to the "home" route for your application.
     *
     * Typically, users are redirected here after authentication.
     *
     * @var string
     */
    public const HOME = '/home';

    protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, and other route configuration.
     */
    public function boot(): void
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::middleware('api')
                    ->prefix('api')
                    ->group(base_path('routes/api.php'));

            Route::middleware('web')
                    ->namespace($this->namespace)
                    ->group(base_path('routes/web.php'));
        });
    }

    /**
     * Configure the rate limiters for the application.
     */
    protected function configureRateLimiting(): void
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        });
    }

}

This beats having a bunch of use statements in the web.php route file.

1 like
Umair_9's avatar

This is what I am using right now, and it's working fine for me, Not sure if this is good, but it's working. in your app.php ​

use App\Http\Middleware\UserRoles;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        // web: __DIR__ . '/../routes/web.php',
        commands: __DIR__ . '/../routes/console.php',
        health: '/up',

        then: function () {
            Route::middleware('web')
                ->namespace('App\Http\Controllers')
                ->group(base_path('routes/web.php'));
        },
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->alias([
            'UserRoles' => UserRoles::class
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();```
5 likes
vcgnetwork's avatar

@Umair_9 Everyone above missed this as the right answer. I tried all of the above entries iincluding the AI default answer. Your solution worked correctly. Thank you.

Adding variable references and things like it felt wrong to me. I want my code as clean as possible. I do appreciate everyones help though.

Live long and prosper! ;)

saluei's avatar

RouteServiceProvider dose not exist in Laravel 11. to set namespace prefix use the file app.php in bootstrap folder. as follow

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        using: function () {
           
            Route::middleware('web')
                ->namespace('App\Http\Controllers')
                ->group(base_path('routes/web.php'));
        },
        api: __DIR__.'/../routes/api.php ',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    }
    )->create();

more detail in https://laravel.com/docs/master/routing#routing-customization route customization section.

Please or to participate in this conversation.