Inflector

  • class Cake\Utility\Inflector
  • The Inflector class takes a string and can manipulate it to handle wordvariations such as pluralizations or camelizing and is normally accessedstatically. Example:Inflector::pluralize('example') returns “examples”.

You can try out the inflections online at inflector.cakephp.org.

Summary of Inflector Methods and Their Output

Quick summary of the Inflector built-in methods and the results they outputwhen provided a multi-word argument:

MethodArgumentOutput
pluralize()BigAppleBigApples
big_applebig_apples
singularize()BigApplesBigApple
big_applesbig_apple
camelize()big_applesBigApples
big appleBigApple
underscore()BigApplesbig_apples
Big Applesbig apples
humanize()big_applesBig Apples
bigAppleBigApple
classify()big_applesBigApple
big appleBigApple
dasherize()BigApplesbig-apples
big applebig apple
tableize()BigApplebig_apples
Big Applebig apples
variable()big_applebigApple
big applesbigApples
slug()Big Applebig-apple
BigApplesBigApples

Creating Plural & Singular Forms

  • static Cake\Utility\Inflector::singularize($singular)
  • static Cake\Utility\Inflector::pluralize($singular)
  • Both pluralize and singularize() work on most English nouns. If you needto support other languages, you can use Inflection Configuration tocustomize the rules used:
  1. // Apples
  2. echo Inflector::pluralize('Apple');

Note

pluralize() may not always correctly convert a noun that is already in its plural form.

  1. // Person
  2. echo Inflector::singularize('People');

Note

singularize() may not always correctly convert a noun that is already in its singular form.

Creating CamelCase and under_scored Forms

  • static Cake\Utility\Inflector::camelize($underscored)
  • static Cake\Utility\Inflector::underscore($camelCase)
  • These methods are useful when creating class names, or property names:
  1. // ApplePie
  2. Inflector::camelize('Apple_pie')
  3.  
  4. // apple_pie
  5. Inflector::underscore('ApplePie');

It should be noted that underscore will only convert camelCase formatted words.Words that contains spaces will be lower-cased, but will not contain anunderscore.

Creating Human Readable Forms

  • static Cake\Utility\Inflector::humanize($underscored)
  • This method is useful when converting underscored forms into “Title Case” formsfor human readable values:
  1. // Apple Pie
  2. Inflector::humanize('apple_pie');

Creating Table and Class Name Forms

  • static Cake\Utility\Inflector::classify($underscored)
  • static Cake\Utility\Inflector::dasherize($dashed)
  • static Cake\Utility\Inflector::tableize($camelCase)
  • When generating code, or using CakePHP’s conventions you may need to inflecttable names or class names:
  1. // UserProfileSetting
  2. Inflector::classify('user_profile_settings');
  3.  
  4. // user-profile-setting
  5. Inflector::dasherize('UserProfileSetting');
  6.  
  7. // user_profile_settings
  8. Inflector::tableize('UserProfileSetting');

Creating Variable Names

  • static Cake\Utility\Inflector::variable($underscored)
  • Variable names are often useful when doing meta-programming tasks that involvegenerating code or doing work based on conventions:
  1. // applePie
  2. Inflector::variable('apple_pie');

Creating URL Safe Strings

  • static Cake\Utility\Inflector::slug($word, $replacement = '-')
  • Slug converts special characters into latin versions and converting unmatchedcharacters and spaces to dashes. The slug method expects UTF-8 encoding:
  1. // apple-puree
  2. Inflector::slug('apple purée');

Note

Inflector::slug() has been deprecated since 3.2.7. Use Text::slug()instead.

Inflection Configuration

CakePHP’s naming conventions can be really nice - you can name your databasetable bigboxes, your model BigBoxes, your controllerBigBoxesController, and everything just works together automatically. Theway CakePHP knows how to tie things together is by _inflecting the wordsbetween their singular and plural forms.

There are occasions (especially for our non-English speaking friends) where youmay run into situations where CakePHP’s inflector (the class that pluralizes,singularizes, camelCases, and under_scores) might not work as you’d like. IfCakePHP won’t recognize your Foci or Fish, you can tell CakePHP about yourspecial cases.

Loading Custom Inflections

  • static Cake\Utility\Inflector::rules($type, $rules, $reset = false)
  • Define new inflection and transliteration rules for Inflector to use. Often,this method is used in your config/bootstrap.php:
  1. Inflector::rules('singular', ['/^(bil)er$/i' => '\1', '/^(inflec|contribu)tors$/i' => '\1ta']);
  2. Inflector::rules('uninflected', ['singulars']);
  3. Inflector::rules('irregular', ['phylum' => 'phyla']); // The key is singular form, value is plural form

The supplied rules will be merged into the respective inflection sets defined inCake/Utility/Inflector, with the added rules taking precedence over the corerules. You can use Inflector::reset() to clear rules and restore theoriginal Inflector state.