Table of Contents
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.
Settings Environment Variables
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.
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.
Changes a string of words separated by spaces or underscores to camel case.
echo camelize('my_dog_spot'); // Prints 'myDogSpot'
This function changes a word and its count to a phrase.
echo counted(3, 'dog'); // Prints '3 dogs'
It replaces underscores with dashes in the string.
echo dasherize('hello_world'); // Returns 'hello-world'
It takes multiple words separated by underscores and adds spaces between them. Each word is capitalized.
humanize($string[, $separator = '_'])
echo humanize('my_dog_spot'); // Prints 'My Dog Spot'
To use dashes instead of underscores:
echo humanize('my-dog-spot', '-'); // Prints 'My Dog Spot'
This function checks if the given word has a plural version.
echo is_pluralizable('equipment'); // Returns FALSE echo is_pluralizable('dogs'); // Returns TRUE
This function changes a string of words separated by spaces or underscores to Pascal case, which is camel case with the first letter capitalized.
echo pascalize('my_dog_spot'); // Prints 'MyDogSpot'
This function changes a singular word to plural
echo plural('dog'); // Prints 'dogs'
This function changes a plural word to singular.
echo singular('dogs'); // Prints 'dog'
This function takes multiple words separated by spaces and underscores them.
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.
Find More on CodeIgniter 4 here
- CodeIgniter 4 Cookie Helper Tutorial
- CodeIgniter 4 CRUD Application Tutorial
- CodeIgniter 4 CRUD REST APIs Tutorial
- CodeIgniter 4 CSRF Token in AJAX Request
- Database Query in CodeIgniter 4 Tutorial
- CodeIgniter 4 Ajax Form Data Submit
- CodeIgniter 4 Form Validation Tutorial
- CodeIgniter 4 Image Upload with Form Tutorial
- Multi language in CodeIgniter 4 Tutorial
- Stripe Payment Gateway Integration in CodeIgniter 4
- CodeIgniter 4 CSRF Token Tutorial
- CodeIgniter 4 Basics Tutorial
- CodeIgniter 4 Spark CLI Commands Tutorial
- Migration in CodeIgniter 4 Tutorial
- Seeders in CodeIgniter 4 Tutorial
Hi, I am Sanjay the founder of ONLINE WEB TUTOR. I welcome you all guys here to join us. Here you can find the web development blog articles. You can add more skills in web development courses here.
I am a Web Developer, Motivator, Author & Blogger. Total experience of 7+ years in web development. I also used to take online classes including tech seminars over web development courses. We also handle our premium clients and delivered up to 50+ projects.