How to Integrate Faker as a Third Party into Codeigniter

Faker is one of the most popular PHP library for generating fake data. I quite often use this library at the start of my project. Laravel has built-in faker library but for codeigniter I have to add faker as a third party library.

So in this post I am going to show you how to integrate faker into codeigniter.

Integrate Faker into Codeigniter:

Following are the steps involves in integration process:

  • Download codeigniter 3.x from https://codeigniter.com/download
  • Download or clone faker library from https://github.com/fzaninotto/Faker
  • Copy faker src folder fo the codeigniter->application->thirdparty and rename src folder as faker.
  • Add include path in the controller in this example I will use Welcome.php controller.
  • Use Faker\Factory::create() to create and initialize faker

Codeigniter Folder Structure:

After downloading default codeigniter structure is something like this.

codeigniter folder structure

Faker Folder Structure:

After downloading from github faker folder structure look like below.

faker folder structure

Copy Faker Src folder to application/third_party:

Now copy faker src folder to codeigniter/application/third_party folder and rename src folder to faker.

third party faker

Add Include path to the Controller (controllers/welcome.php):

Copy below Path to index function of welcome.php

Now initialize and use faker by assigning it to the variable.

Your final index function will look something like this

Also read:

 

Leave a Reply

Your email address will not be published. Required fields are marked *