Concept of Inflector Helper in CodeIgniter 4 Tutorial

Reading Time: 5 minutes
2,633 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.

Learn More –

Let’s get started.


CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Environment (.env) Setup

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

Either we can do via renaming file as simple as that. Also we can do by terminal command.

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.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

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

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more