7

Question about routing in Laravel 8.x

Now Im adding such lines in web.php file:

use App\Http\Controllers\FirstController;
use App\Http\Controllers\SecondController;
use App\Http\Controllers\ThirdController;

and then just working on FirstController::class etc.

Is it wrong to use just namespace App\Http\Controllers; instead of all use lines one after another x times at the beggining?

Thanks.

Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
Arek TG
  • 190
  • 1
  • 1
  • 9
  • 1
    if you declare a namespace in that route file you would have to adjust the call to `Route` and the like but I suppose you could .... you could also alias that namespace and then reference the class via `Controllers\ThirdController::class` ... but i wouldn't be too worried about using `use` statements – lagbox Sep 24 '20 at 00:38

1 Answers1

10

Instead, I would simply uncomment this line in app/Providers/RouteServiceProvider.php which will revert back to the Laravel <8 behavior of automatically prefixing route declarations in 'routes/web.php' 'routes/api.php' with the App\Http\Controllers namespace.

/**
 * The controller namespace for the application.
 *
 * When present, controller route declarations will automatically be prefixed with this namespace.
 *
 * @var string|null
 */
 protected $namespace = 'App\\Http\\Controllers';

This commented out property might not be in your app/Providers/RouteServiceProvider.php if you created the project when v8 was first released, (seems it was removed then added back) if not, just add it and uncommnet, and make sure its used in the boot method the prop and it'll work.

public function boot()
{
    $this->configureRateLimiting();

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

        Route::middleware('web')
            ->namespace($this->namespace)            // make sure this is present
            ->group(base_path('routes/web.php'));
    });
}
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • 1
    only if the route groups in that provider are adding a namespace using that member variable, which is the actual important part – lagbox Sep 24 '20 at 00:12
  • @lagbox indeed, updated to be more clear on that since it wasnt there in the initial release – Wesley Smith Sep 24 '20 at 00:15
  • 1
    yea i think there was enough backlash that they had to add this stuff back in and make it easier to change back to the old method; which should be the default in the first place with the option to "null" out that instead to get the current default behavior of 8 – lagbox Sep 24 '20 at 00:16
  • @lagbox 100% agreed! very odd to have axed it. – Wesley Smith Sep 24 '20 at 00:17
  • 1
    Though it solves the problem of repeatation, It defeates the purpose it was intended for. All the fuzz about the change was to help IDEs, You could jump to the definition directly, But enabling the old settings, you cannot jump to definition as its now a string. very handy feature IMHO. BUT, What I would like is namespace instead, so i don't have to repeat myself. – Abdul Rehman Jul 02 '21 at 10:54