How to create and use library functions in CodeIgniter?
Please follow the steps below:
- Open file manager in cPanel, and go to the installation folder.
- Go inside the libraries folder under the application folder.
- Create a simple library file, Test function.
- Define a class, with the same name, as the file name.
- Inside the class, create a simple function, which will return the current date.
- To make the library function available globally, you need to add the file name to the autoload.php file.
- Now create a Controller file, Test.
- Write a simple function, to save the output from the library function, and pass it to the loaded view file called test.
- Create a view file as test, to display the output of the test controller.
- Now, check the output.
- This will display the current date.
- Now, write the library function directly to the view file.
- The same output is displayed.
- Delete the library function, and the variable from the controller function.
The output is still the same.
- This shows that the library functions can be used in view files directly, instead of passing a value through the controller.
- This way you can create and use library functions in PHP CodeIgniter.