简体   繁体   中英

Laravel correct way to name and define routes?

Example 1:

Route::get('/', 'API\PostController@index')->name(‘post.index’);

Example 2:

Route::get('/', [PostController::class, 'index'])->name(‘post.index’);

In this example we need to use the following use statement at the top of the route file:

use App\Http\Controllers\API\PostController;

The second example makes it easy to navigate across the controllers from the route file with a cmd + click but compared to better performance which is the best ?

Is it must to define named routes when using the laravel only as a API, can it add more overhead to overall performance ?

There isn't really a performance difference because you cache the routes in production with these commands: php artisan optimize or php artisan route:cache

Pick the one you prefer the most.

https://laravel.com/docs/6.x/controllers#route-caching

It doesn't really matter which one you choose to define your routes but it's recommended to use route caching on production:

If your application is exclusively using controller based routes, you should take advantage of Laravel's route cache. Using the route cache will drastically decrease the amount of time it takes to register all of your application's routes. In some cases, your route registration may even be up to 100x faster. To generate a route cache, just execute the route:cache Artisan command:

php artisan route:cache

Route::get('/', 'API\\PostController@index')->name('post.index');

try this hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM