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.
Learn More –
- Image Manipulation Class in CodeIgniter 4 Tutorial
- Image Upload with Form data in CodeIgniter 4 Tutorial
- Image Upload with Preview Using Ajax in CodeIgniter 4
- Implementation of CodeIgniter 4 CSRF Token
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.
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.
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.