php Laravel 5 命名约定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41655535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:26:06  来源:igfitidea点击:

Laravel 5 Naming Conventions

phplaravellaravel-5

提问by Yohan Hirimuthugoda

I am bit confused with Laravel conventions as I am new to this framework. I am following Jeffrey Way Laracasts videos he uses Pluralfor Controller names.

我对 Laravel 约定有点困惑,因为我是这个框架的新手。我正在关注 Jeffrey Way Laracasts 视频,他使用复数作为控制器名称。

E.g.: PagesController, Cards Controller, PostsController

例如:PagesController、卡片控制器、PostsController

But if I refer official documentations of Laravel > Controllersand Laravel > Tutorials > Quick Start > Intermediate Task Listit uses Singularnames.

但是如果我参考Laravel > ControllersLaravel > Tutorials > Quick Start > Intermediate Task List 的官方文档,它使用单数名称。

E.g.: PhotoController, TaskController

例如:PhotoController、TaskController

Can anybody please list down the official coding conventions for following entities?

任何人都可以列出以下实体的官方编码约定吗?

Tables: posts, comments, post_commentsor Post, Comment, PostComment

表:posts、comments、post_commentsPost、Comment、PostComment

Columns: id, post_id, comment_idor id, postId, commentId

列:id、post_id、comment_idid、postId、commentId

Controllers: PagesController, Cards Controller, PostsControlleror PhotoController, TaskController

控制器:PagesController、卡片控制器、PostsControllerPhotoController、TaskController

Models: Pages, Cards, Postsor Page, Card, Post

模型:页面、卡片、帖子页面、卡片、帖子

回答by Alexey Mezenin

Tables:posts, comments, comment_post

表:帖子、评论、comment_post

Columns:id, post_id, comment_id

列:id、post_id、comment_id

Controllers:PhotoController, TaskController

控制器:PhotoController、TaskController

Models:Page, Card, Post

模型:页面、卡片、帖子

For more details check out my Laravel naming conventionstable.

有关更多详细信息,请查看我的Laravel 命名约定表。

回答by paolobasso

Remember that "conventions" are just conventions and you could do whatever you want just be constant, however it's better follow the documentation:

请记住,“约定”只是约定,您可以做任何您想做的事情,只要保持不变,但最好遵循文档:

  1. Table name: plural and _to separate words (users, tags, ...)
  2. Columns name: singular and _to separate words (user, tag, ...)
  3. Models: singular with first letter capitalized and capitalization to separate words (User, Tag, ...)
  4. Controllers: singular with capitalized first letter and capitalization to separate words followed by "Controller" (UserController, TagController, ...)
  1. 表名:复数并_分隔单词(用户、标签等)
  2. 列名称:单数和_分隔单词(用户、标签、...)
  3. 模型:单数,首字母大写,大写分隔单词(用户,标签,...)
  4. 控制器:单数,首字母大写,大写分隔单词,后跟“控制器”(UserController, TagController, ...)

回答by Madmarsu

I know I'm from old school (coding since 1984 on computers, though having evolved with PHP and js/ECMAScript since their first version), but the good old Merise rule "never use plural" had strong and good resasons to be respected. What a pity Eloquent forces us to use table name in plural.

我知道我来自老派(自 1984 年以来在计算机上编码,尽管自 PHP 和 js/ECMAScript 的第一个版本以来一直在发展),但是古老的 Merise 规则“永远不要使用复数”具有强烈而良好的理由值得尊重。可惜 Eloquent 强迫我们使用复数形式的表名。

PSR-2 or PSR-1 doesn't indicate anything about plural or singular, but for simplicity sake, I heavily recommend to always use singular, excepte where the "system" needs it (as Eloquent does). In that case, don't mix plural and singular. On the datatables used by laravel, we use table names in plural. That's a singularity (compared to the other developments already made or with which we communicate), but all the tables are in plural on that part.

PSR-2 或 PSR-1 不表示任何关于复数或单数的内容,但为了简单起见,我强烈建议始终使用单数,除非“系统”需要它(如 Eloquent 那样)。在这种情况下,不要混合使用复数和单数。在 Laravel 使用的数据表上,我们使用复数形式的表名。这是一个奇点(与已经完成或我们与之交流的其他发展相比),但所有表格在这部分都是复数形式。

And NEVER make a typo when naming something (example: 'personnal' i.o. 'personal', etc.). Doublecheck first. Orthograph rules.

并且在命名某些东西时永远不要打错字(例如:'personnal' io 'personal' 等)。首先仔细检查。正字法规则。