Add Faker to CodeIgniter to Generate Fake or Dummy Data

Faker is a PHP library that generates dummy data. It is useful for developers who need to bootstrap MySQL database, create validated XML or JSON documents, and many more.

In this post we will take a look at how to integrate this library into CodeIgniter (CI) 3. You should have a working copy of CodeIgniter 3 before anything else.

Faker supports both PSR-0 as PSR-4 autoloaders so we can add it to CI using PSR-0 method.

  1. Add Faker library from https://github.com/fzaninotto/Faker
  2. Copy it to application\third_party\faker.
  3. Include library in any controllers that requires it include APPPATH . 'third_party/faker/autoload.php';
  1. Use Faker\Factory::create() to create and initialize faker
include APPPATH . 'third_party/faker/autoload.php';	
$faker = Faker\Factory::create();

$data = array(	
	'fullname' => $faker->firstName.' '.$faker->lastName,
	'age' => $faker->numberBetween(20, 60),
	'tel' => $faker->phoneNumber,
	'email' => $faker->email,
);

$data2 = array(
	'city' => $faker->city,
	'color' => $faker->hexcolor(),
	'description' => $faker->sentence($nbWords = 6, $variableNbWords = true)
);

Leave a Comment

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

We care about your privacy and data security. Google serves cookies to analyse traffic and show ads on this site. By using this site you agree to these cookies being set. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close