简体   繁体   English

Laravel 命名和定义路由的正确方法?

[英]Laravel correct way to name and define routes?

Example 1:示例 1:

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

Example 2:示例 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 语句:

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 ?第二个示例可以通过 cmd + 单击轻松从路由文件中导航控制器,但与更好的性能相比,哪个最好?

Is it must to define named routes when using the laravel only as a API, can it add more overhead to overall performance ?仅将 laravel 用作 API 时是否必须定义命名路由,是否会为整体性能增加更多开销?

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没有真正的性能差异,因为您使用以下命令缓存生产中的路由: php artisan optimizephp artisan route:cache

Pick the one you prefer the most.选择你最喜欢的一个。

https://laravel.com/docs/6.x/controllers#route-caching 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.如果你的应用专门使用基于控制器的路由,你应该利用 Laravel 的路由缓存。 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.在某些情况下,您的路线注册速度甚至可能快 100 倍。 To generate a route cache, just execute the route:cache Artisan command:要生成路由缓存,只需执行route:cache Artisan 命令:

php artisan route:cache

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

try this hope it helps.试试这个希望它有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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