What are the Laravel naming conventions for controllers/models/views?

青春壹個敷衍的年華 提交于 2019-12-03 09:34:05

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.

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 Group standards. Read more on them on their page to find out more.

Laravel 4 will follow all of the standards (PSR-0, PSR-1 and PSR-2), but Laravel 3 isn't. For example: it doesn't use camel case for methods which is "required" by PSR-1.

The convention is:

  • Model class names are singular (class Photo extends model)
  • table names are plural (select id from photos)
  • controller resource names are singular (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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!