CakePHP naming conventions and the mighty Inflector!
I regularly have problems finding the correct names for my controllers, models, views and tables. How can that be so hard, one might think.
Well, CakePHP does a lot of tedious and repetive work for you, so you can concentrate on the bigger picture. But CakePHP relies on you sticking to its naming conventions!
Hmm, sticking to naming conventions can't be to hard, but it kinda is, when english is not your primary language and CakePHP expects you to correctly convert between singular, plural, etc. ;)
A quote from the CakePHP conventions:
Model classnames are singular and CamelCased. Person, BigPerson, and ReallyBigPerson are all examples of conventional model names.
Table names corresponding to CakePHP models are plural and underscored. The underlying tables for the above mentioned models would be people, big_people, and really_big_people, respectively.
What? You might think. CakePHP even expects you to substitute persons for people, and such?
Apparently CakePHP converts even irregular nouns properly and i asked myself; how? Does it have a massive look-up-table for all of 'em? Well no! Instead it does some pretty sophisticated stuff in a class called cake/libs/inflector.php, with a lot of regex-pattern matching and more! As you can see in Line 210 it even handles penises! I always thought the plural of peniswould be penii. CakePHP obviously knows better. Anyways: Disgusting! I am shocked ;) Or is CakePHP a girl? I don't know. Either way CakePHP.org should consider a color-theme makeover to reflect it's true personality.
As guessing the correct names is (even with my new gained knowledge about the inflector class) still to hard for my otherwise super awesome brain, i now let the inflector-class do the guessing for me.
// Ask the static Inflector class for the right names: // Let's start with some arbitrary singular model class name $modelName = 'AwesomeNebula'; $controllerName = Inflector::pluralize($modelName) . 'Controller'; $controllerFileName = Inflector::underscore($modelName . 'Controller'); $urlName = '/' . Inflector::underscore($modelName); $tableName = Inflector::tableize($modelName); $viewName = Inflector::tableize($modelName); echo $modelName; // echoes "AwesomeNebula" // singular echo $controllerName; // echoes "AwesomeNebulaeController" // plural echo $controllerFileName; // echoes "awesome_nebulae_controller.php" echo $urlName; // echoes "/awesome_nebulae" echo $tableName; // echoes "awesome_nebulae" // etc...
Actually the Inflector class can do some pretty nifty things, so you should consider asking it before trying to figure it out yourself.
Finally, here is short overview of the Inflector classes methods. A similiar listing can be found at the Inflector Class Methods Overview or in slightly greater detail at the Inflector class API Description.
- Pluralizes a noun:
Apple, Man → Apples, Men
- Singularizes a noun:
Apples, Men → Apple, Man
Converts underscored_words to camelCased:
hadron_collider → hadronCollider
- Converts camelCased to lowercase underscored_words:
HadronCollider → hadron_collider
- Converts underscored words into human readable format:
devine_intervention → Devine Intervention
- Converts from CamelCased singular nouns to plural underscored:
MajorFeatureThing → major_feature_things
- Converts from underscored plural or singular to singular CamelCased with an uppercase Capital:
major_feature_things → MajorFeatureThing
- Converts from underscored plural or singular to singular CamelCased with an lowercase Capital:
major_feature_things → majorFeatureThing
- Converts special characters into latin versions and replaces whitespaces and unknown characters to underscores. This method expects UTF-8 encoding!
apple purée → apple_puree
Now, go! Go and tell all your friends about your fancy new wisdom!