控制器/模型/视图的 Laravel 命名约定是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13539999/
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
What are the Laravel naming conventions for controllers/models/views?
提问by qwerty
I remember hearing you should name your controllers, models and views in a special way. Either singular or plural. I don't remember which ones to name what though, and i can't find anything about it in the doc.
我记得听说你应该以特殊的方式命名你的控制器、模型和视图。无论是单数还是复数。我不记得要命名哪些名称,而且我在文档中找不到任何关于它的信息。
I'm guessing it's like this:
我猜是这样的:
- Controllers are plural
- Views are plural
- Models are singular
- 控制器是复数
- 观点是复数
- 型号单一
Am i on the right track?
我在正确的轨道上吗?
I understand it's just a convention and you don't haveto follow them, but i still want to know what the right way is.
我明白这只是一个约定,你不要有跟着他们,但我还是想知道正确的方法是什么。
采纳答案by armen.shimoon
In ASP.NET MVC, I use the convention you mentioned above, expect for Views, which are mixed. If I have a view that displays multiple "things", such as a list of Employees, it is plural. If I have a view that displays a single Employee, it is singular.
在 ASP.NET MVC 中,我使用你上面提到的约定,除了视图,它是混合的。如果我有一个显示多个“事物”的视图,例如员工列表,则它是复数形式。如果我有一个显示单个员工的视图,它是单数。
回答by JohanTux
The convention is:
公约是:
- Model class names are singular (class Photoextends model)
- table names are plural (select id from photos)
- controller resource names are singular (PhotoController.php)
- 模型类名是单数(类Photo扩展模型)
- 表名是复数(从照片中选择 id )
- 控制器资源名称是单数(PhotoController.php)
I couldn't find the convention for controller names defined in the documentation, but all documented examples place the controller resource name in the singular.
我找不到文档中定义的控制器名称约定,但所有记录的示例都将控制器资源名称放在单数形式中。
From the Laravel 5.5 documentation:
By convention, the "snake case", plural name of the class will be used as the table name unless another name is explicitly specified... Eloquent will assume the Flight model stores records in the flights table
按照惯例,除非明确指定另一个名称,否则将使用类的复数名称“snake case”作为表名...... Eloquent 将假设 Flight 模型将记录存储在航班表中
回答by Niklas Modess
It doesn't matter what you name them actually. It's just a matter of taste as long as you do it consistently. Sometimes you won't even have an option but to follow an already determined code style by a current project.
你实际上给它们起什么名字并不重要。只要您始终如一地这样做,这只是品味问题。有时,您甚至别无选择,只能遵循当前项目已经确定的代码风格。
One good practice is that if you can, is to follow the PHP Framework Interop Groupstandards. Read more on them on their page to find out more.
一种好的做法是,如果可以,请遵循PHP Framework Interop Group标准。在他们的页面上阅读更多关于他们的信息以了解更多信息。
Laravel 4 will follow all of the standards (PSR-0, PSR-1and PSR-2), but Laravel 3 isn't. For example: it doesn't use camel case for methods which is "required" by PSR-1.
Laravel 4 将遵循所有标准(PSR-0、PSR-1和PSR-2),但 Laravel 3 不是。例如:对于PSR-1“需要”的方法,它不使用驼峰式大小写。
回答by Neo
The answers here are pretty correct but if you are talking about Laravel, you should name your Models class in the singular form because of Laravel built in features for instance the Eloquent class is clever enough that it can detect the plurals for the English language. So if our object is singular it will use the plural form of that name to access the database table for that object.
这里的答案非常正确,但如果你在谈论 Laravel,你应该以单数形式命名你的 Models 类,因为 Laravel 内置功能,例如 Eloquent 类足够聪明,它可以检测英语的复数形式。因此,如果我们的对象是单数,它将使用该名称的复数形式来访问该对象的数据库表。