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

twiggy99999's avatar

Better way of routing in Laravel 8 without all the use statements?

I think I'm missing the obvious here because everyone has been praising the new routing format.

For me, my route files have now doubled in size because for every route I bring in I also have to bring in the use statement for the class being called at the top of the file.

I can only assume I'm doing this wrong or I'm missing something obvious? Is there another way to do this without having a use include for every single route line?

  • P.S I know I can enable namespacing again in the route service provider but I want to stick to the new convention if it's possible, my only concern is the size of the route file.
0 likes
3 replies
MichalOravec's avatar

Three options:

use App\Http\Controllers\UserController;

Route::get('user/{id}', [UserController::class, 'show']);

Or

Route::get('user/{id}', [\App\Http\Controllers\UserController::class, 'show']);

Or Laravel 7 way (I prefer this one).

Route::get('user/{id}', 'UserController@show');

Laravel 8 routes are useful only for PhpStorm users.

2 likes
topvillas's avatar

You could put the full namespace path instead of using use.

Either way, you'll have to put the namespace somewhere.

I'd stop worrying about it. Use code folding in your IDE to hide all the imports, if it really bothers you.

Snapey's avatar

From the docs

If you would like to continue using the original auto-prefixed controller routing, you can simply set the value of the $namespace property within your RouteServiceProvider and update the route registrations within the boot method to use the $namespace property:

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to the "home" route for your application.
     *
     * This is used by Laravel authentication to redirect users after login.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * If specified, this namespace is automatically 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()
    {
        $this->configureRateLimiting();

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

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

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60);
        });
    }
}
2 likes

Please or to participate in this conversation.