Concept of Inflector Helper in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 5 minutes
729 Views

The Inflector Helper file contains functions that allow us to change English words to plural, singular, camel case, etc. This is a pre defined helper available in CodeIgniter 4.

Inside this article we will see the concept of Inflector in CodeIgniter 4. All the functions available here to use will see in great detail.

Helpers in CodeIgniter are standalone functions which helps in application to complete some specific task.

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system.

To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Turn Development Mode On

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

 // Do it to 
 
CI_ENVIRONMENT = development

Now application is in development mode.


What is Inflector Helper?

Inflector helper provides several functions which works with string value and works for basic general operations.

This helper file inflector_helper.php we can find at /vendor/codeigniter4/framework/system/Helpers folder.

Here, we have the functions available when we use Inflector helper.

  • camelize()
  • counted()
  • dasherize()
  • humanize()
  • is_pluralizable()
  • ordinal()
  • ordinalize()
  • pascalize()
  • plural()
  • singular()
  • underscore()

First before using any functions, we need to load inflector helper and then we can access all of it’s functions.


Loading Inflector Helper

We can load inflector helper in CodeIgniter 4 in two different ways. Methods to load by –

  • By BaseController.php [parent controller]
  • Loading into any specific controller

Loading by Parent Controller

Open BaseController.php file from /app/Controllers folder and search for $helpers. Add helper into it.

protected $helpers = ["inflector"];

Loading into Specific Controller

Open any application controller, go inside method and write this code to load inflector helper.

helper("inflector");

OR

helper(["inflector"]);

Inflector Helper Functions

We will see all inflector functions here.

camelize()

Changes a string of words separated by spaces or underscores to camel case.

Syntax

camelize($string)

Example

echo camelize('my_dog_spot'); // Prints 'myDogSpot'

counted()

This function changes a word and its count to a phrase.

Syntax

counted($count, $string)

Example

echo counted(3, 'dog'); // Prints '3 dogs'

dasherize()

It replaces underscores with dashes in the string.

Syntax

dasherize($string)

Example

echo dasherize('hello_world'); // Returns 'hello-world'

humanize()

It takes multiple words separated by underscores and adds spaces between them. Each word is capitalized.

Syntax

humanize($string[, $separator = '_'])

Example #1

echo humanize('my_dog_spot'); // Prints 'My Dog Spot'

To use dashes instead of underscores:

Example #2

echo humanize('my-dog-spot', '-'); // Prints 'My Dog Spot'

is_pluralizable()

This function checks if the given word has a plural version.

Syntax

is_pluralizable($word)

Example

echo is_pluralizable('equipment'); // Returns FALSE

echo is_pluralizable('dogs'); // Returns TRUE

pascalize()

This function changes a string of words separated by spaces or underscores to Pascal case, which is camel case with the first letter capitalized.

Syntax

pascalize($string)

Example

echo pascalize('my_dog_spot'); // Prints 'MyDogSpot'

plural()

This function changes a singular word to plural

Syntax

plural($string)

Example

echo plural('dog'); // Prints 'dogs'

singular()

This function changes a plural word to singular.

Syntax

singular($string)

Example

echo singular('dogs'); // Prints 'dog'

underscore()

This function takes multiple words separated by spaces and underscores them.

Syntax

underscore($string)

Example

echo underscore('my dog spot'); // Prints 'my_dog_spot'

We hope this article helped you to Complete Concept of Inflector Helper in CodeIgniter 4 Tutorial. in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here