简体   繁体   中英

PHP 's' in namespace: Controller or Controllers, Model or Models?

For example Symfony uses \\Controller . Yii2 uses \\controllers and \\models .

Is there a standard about ...s|es like PSR?

None of the PSRs specify whether to use singular or plural in namespaces. This is usually a convention by the framework. The same goes for capitalization.

Whether you can use your preferred naming convention - if you have any - in the framework of your choosing mostly depends on the framework itself and might even vary within parts of the framework. In the case of Symfony it is singular App\\Controller by default, but it's easily changed to App\\Controllers or App\\controllers if you like as long as your routing configuration uses the correct name.

When deviating from the framework's convention you might run into problems, because of the expectations it has when configuring your app. For example Symfony 3 autoloads your Commands when they are placed in the Command/ folder and the class name is suffixed with Command . When you deviate from this you have to manually register them. On the other hand Symfony 4 uses the new autiwiring and autoconfigure defaults in the service configuration and will detect commands based on whether they implement the correct interface - either directly or indirectly by extending the base Command-class - and therefore can have whatever name you like and can be put in any folder. So even within the framework or between different versions of the framework changing the name to something other than the recommendation might have different repercussions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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