简体   繁体   English

Laravel在route.php中是否有组织路由的约定?

[英]Does Laravel have a convention for organizing routes in routes.php?

I was contributing to a project that now has a routes.php file with like over 800 lines of routes. 我为一个项目提供了帮助,该项目现在具有一个route.php文件,其中包含800多行路由。 I was wondering if Laravel had a specific convention or a way to organize this so it doesn't have to have all routes in one large confusing file? 我想知道Laravel是否有特定的约定或组织此约定的方法,以便不必将所有路由都放在一个大的混乱文件中?

Separate routes for large applications into multiple smaller route partial files that are automatically loaded at runtime. 将大型应用程序的路由分成多个较小的路由部分文件,这些文件在运行时会自动加载。

Also you can 你也可以

  1. Group routes 团体路线
  2. Route namespaces 路由名称空间
  3. Route prefixes 路由前缀

Useful examples 有用的例子

partial routes 部分路线

split routes in different parts 在不同部分分割路线

organize routes 整理路线

explained route using 路线说明

break routes 中断路线

Also i can recommend route caching for performance 我也可以推荐路由缓存以提高性能

Route caching tutorial 路由缓存教程

The best ways are groups and don't list every single route but use controller routes. 最好的方法是分组,不要列出每条路由,而要使用控制器路由。 http://laravel.com/docs/4.2/routing#route-groups http://laravel.com/docs/4.2/controllers#implicit-controllers http://laravel.com/docs/4.2/routing#route-groups http://laravel.com/docs/4.2/controllers#implicit-controllers

If this doesn't help it's possible to split this file into multiple files, for example: routes/users.php, routes/posts.php and so on, and include these into the original routes.php 如果这样做没有帮助,则可以将此文件拆分为多个文件,例如:routes / users.php,routes / posts.php等,并将其包含在原始的route.php中

Also it would help to put some routes in different files. 另外,将一些路由放在不同的文件中也将有所帮助。 For example, you could create a folder called routes in your app folder, then add files in that folder. 例如,您可以在应用程序文件夹中创建一个名为route的文件夹,然后在该文件夹中添加文件。 I usually have one file for authentication routes, then other files for the high level navigation items of the site. 我通常有一个用于身份验证路由的文件,然后有一个用于站点高级导航项目的文件。

don't forget to require those files in app/start/global.php 不要忘记在app / start / global.php中要求这些文件

/*
 |-------------------------------------------------------------------------
 | in app/start/global.php
 |-------------------------------------------------------------------------
 |
 */

require app_path( 'routes/auth.php'); // Auth routes
require app_path( 'routes/articles.php'); // Article routes

maybe you should also consider using controller along side this technique to further slim you routes 也许您还应该考虑在此技术旁边使用控制器来进一步简化您的路线

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

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