Not seeing what Laravel Router Namespace will do

I found the setting in Preferences (Mac) > Languages & Frameworks > PHP > Laravel, but I'm not seeing any documentation on it:

How is this supposed to work exactly?

1

This comes from 3rd party Laravel Plugin plugin

My guess, it allows to use another namespace instead of default "App\Http\Controllers" (check RouteServiceProvider class).

2

Our namespace is commented out for some reason:

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

In class RouteServiceProvider extends ServiceProvider, `namespace App\Providers;` , in filepath app/Providers/RouteServiceProvider.php.

All but one of our controllers are in `namespace App\Http\Controllers\Admin` (there's one above that, but it appears to be unused. I don't see a namespace defined anywhere, so I'm not sure how this is actually working 😆

Maybe I'm misunderstanding what you're saying? I wonder if I should put `App\Http\Controllers\Admin` in the Router Namespace field.

0

It is commented out in newer Laravel versions (v8 for sure) since they have moved to using modern "use" statements for classes.

In previous Laravel versions (v5.x..7 at very least) it was uncommented and you by default were suggested to use the following:

https://laravel.com/docs/7.x/routing#basic-routing

Route::get('/active-week', 'API\Deals\ListActiveDealsController@handle');

Where API\Deals\ListActiveDealsController would be resolved during runtime to the full class name of  \App\Http\Controllers\API\Deals\ListActiveDealsController (the $namespace + the class).

In modern Laravel versions (v8) the recommended way is to use the use statements and ::class so no guesswork is involved at all:

https://laravel.com/docs/8.x/routing#the-default-route-files

use App\Http\Controllers\API\Deals\ListActiveDealsController;

Route::get('/active-week', [ListActiveDealsController::class, 'handle']);

That option in Laravel plugin settings allows to specify custom $namespace for those older Laravel versions (a thing to remember: Laravel plugin is quite old / was created a few years ago and rarely sees updates these days).

So for a modern approach (as recommended in Laravel 8 docs) you do not need that at all as a full class name is used right there.

P.S. You can even mix 2 styles together -- I have one project where Laravel 5.8 is used (it's an old internal site, so why not). And I have a few routes declared using ::class and the rest using old style -- works just fine (the actual code + navigation in the PhpStorm).

 

Maybe I'm misunderstanding what you're saying? I wonder if I should put `App\Http\Controllers\Admin` in the Router Namespace field.

If it's modern Laravel with ::class then no need to put anything.

If it's old style -- make sure that $namespace is uncommented .. and no need to put anything in Laravel plugin (as it's a default namespace anyway)

0

请先登录再写评论。