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.
Like this:

	// 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.

pluralize
Pluralizes a noun:

Apple, Man → Apples, Men

singularize
Singularizes a noun:

Apples, Men → Apple, Man

camelize
Converts underscored_words to camelCased:

hadron_collider → hadronCollider

underscore
Converts camelCased to lowercase underscored_words:

HadronCollider → hadron_collider

humanize
Converts underscored words into human readable format:

devine_intervention → Devine Intervention

tableize
Converts from CamelCased singular nouns to plural underscored:

MajorFeatureThing → major_feature_things

classify
Converts from underscored plural or singular to singular CamelCased with an uppercase Capital:

major_feature_things → MajorFeatureThing

variable
Converts from underscored plural or singular to singular CamelCased with an lowercase Capital:

major_feature_things → majorFeatureThing

slug
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!

Comments

Re your first point about

Re your first point about editing in a browser - user Firefox with addon Lazarus - has saved me a few times.

Re your first point about

Thx for the hint :) I will give it a try. Although i try not to edit in the browser in the first place.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <blockquote> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><pre> <h1> <h2> <h3> <h4>
  • Lines and paragraphs break automatically.
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.