简体   繁体   English

Laravel 5命名约定

[英]Laravel 5 Naming Conventions

I am bit confused with Laravel conventions as I am new to this framework. 我对Laravel惯例感到困惑,因为我是这个框架的新手。 I am following Jeffrey Way Laracasts videos he uses Plural for Controller names. 我关注Jeffrey Way Laracasts的视频,他使用Plural for Controller名称。

Eg: PagesController, Cards Controller, PostsController 例如: PagesController,Cards Controller,PostsController

But if I refer official documentations of Laravel > Controllers and Laravel > Tutorials > Quick Start > Intermediate Task List it uses Singular names. 但是,如果我参考Laravel>控制器Laravel>教程>快速入门>中级任务列表的官方文档,它将使用奇异名称。

Eg: PhotoController, TaskController 例如: PhotoController,TaskController

Can anybody please list down the official coding conventions for following entities? 有人可以列出以下实体的官方编码约定吗?

Tables: posts, comments, post_comments or Post, Comment, PostComment 表: 帖子,评论,post_comments帖子,评论,PostComment

Columns: id, post_id, comment_id or id, postId, commentId 列: id,post_id,comment_idid,postId,commentId

Controllers: PagesController, Cards Controller, PostsController or PhotoController, TaskController 控制器: PagesController,Cards Controller,PostsControllerPhotoController,TaskController

Models: Pages, Cards, Posts or Page, Card, Post 型号: 页面,卡片,帖子页面,卡片,帖子

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 conventions table. 有关更多详细信息,请查看我的Laravel命名约定表。

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, ...) 模型 :单数,首字母大写,大写分隔单词(User,Tag,...)
  4. Controllers : singular with capitalized first letter and capitalization to separate words followed by "Controller" (UserController, TagController, ...) 控制器 :带有大写首字母和大小写的单数,用于分隔单词后跟“Controller”(UserController,TagController,...)

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. 我知道我来自旧学校( 自1984年以来在计算机上编码,虽然自第一版以来已经使用PHP和js / ECMAScript进行了编码 ),但是旧的Merise规则“从不使用复数”具有强大且良好的理由得到尊重。 What a pity Eloquent forces us to use table name in plural. 真可惜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). PSR-2或PSR-1并不表示有关复数或单数的任何内容,但为了简单起见,我强烈建议始终使用单数,但“系统”需要的地方(如Eloquent所做)。 In that case, don't mix plural and singular. 在这种情况下,不要混合复数和单数。 On the datatables used by laravel, we use table names in plural. 在laravel使用的数据表中,我们使用复数表名。 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. 这是一个奇点(与已经制作或与之沟通的其他发展相比),但所有表格都是复数形式。

And NEVER make a typo when naming something (example: 'personnal' io 'personal', etc.). 并且在命名时不要写错字(例如:'personnal'io'personal'等)。 Doublecheck first. 先双击。 Orthograph rules. 正射规则。

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

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