When we develop a Application in CodeIgniter , we might be require to make some customized code to use in different controllers, To accomplish this we can create our own libraries.
Table Of Content
1 Prerequisites
1.) PHP version of 8.2
2 Introduction
Creating a custom library in CodeIgniter 4 is a powerful way to extend the framework’s functionality to suit your application's specific needs. Here’s a step-by-step guide on how to create a custom library in CodeIgniter 4.
3 Create / Install a Codeigniter 4 Project
3.1 Install Codeigniter 4 Project
First, make sure your computer has a composer.
Use the following command to install new Codeigniter Project.
After installing CodeIgniter 4, you will have an env file at the root. To use environment variables, rename env to .env using this command:
sudo cp env .env
PHP
Now, configure the development mode by opening the .env file from the root and setting:
# CI_ENVIRONMENT = productionCI_ENVIRONMENT= development
PHP
Now application is in development mode.
4 Create Own Library
To create a custom library in CodeIgniter 4, create a class file inside the app/Libraries directory. Below is a sample code for a custom library named MyLibrary.php:
Open MyLibrary.php file and write this code into it.
Next, create a new controller to use the custom library. Run the following command to create a controller:
phpspark make:controller AccountController
.properties
In the newly created AccountController app/Controllers/AccountController.php, add this code to load the library and use it:
<?phpnamespaceApp\Controllers;useApp\Controllers\BaseController;useApp\Libraries\MyLibrary;// Load Our Custom libraryclassAccountControllerextendsBaseController{publicfunctionindex(){$mylib=newMyLibrary();// create an instance of Library$number="79855995.19";$data=['result'=>$this->mylib->convertNumberToWords($number),];returnview('index',$data);}}?>
PHP
6 Create Index View File
Create the index.php view file to display the results of the custom library:
app/Views/index.php
<!DOCTYPEhtml><htmllang="en"><head><metacharset="utf-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1"><title>CodeIgniter 4 How to Create a Custom Library in Codeigniter 4 Application</title><linkrel="stylesheet"href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"><linkrel="stylesheet"href="https://mdbcdn.b-cdn.net/wp-content/themes/mdbootstrap4/docs-app/css/dist/mdb5/standard/core.min.css"><linkrel='stylesheet'id='roboto-subset.css-css'href='https://mdbcdn.b-cdn.net/wp-content/themes/mdbootstrap4/docs-app/css/mdb5/fonts/roboto-subset.css?ver=3.9.0-update.5'type='text/css'media='all'/></head><body><divclass="container-fluid"><divclass="row"><divclass="col-md-3"></div><divclass="col-md-6 mb-4"><divclass="card mb-4"><divclass="card-header py-3"><h5class="mb-0">CodeIgniter 4 How to Create a Custom Library in Codeigniter 4 Application</h5></div><divclass="card-body"><h2><?phpecho$result;?></h2></div></div></div><divclass="col-md-3"></div></div></div><scripttype="text/javascript"src="https://mdbcdn.b-cdn.net/wp-content/themes/mdbootstrap4/docs-app/js/dist/mdb5/standard/core.min.js"></script></body></html>
PHP
7 Define a Route
Define routes for the AccountController in the app/Config/Routes.php file:
Visit the URL: http://localhost:8080/index.php/account
10 Conclusion
With these steps, you can easily create custom libraries in CodeIgniter 4. This allows you to reuse code efficiently across different parts of your application.