Hurry! Save Up to 50% On Your Purchase | March 2021 X

How to create and use library functions in CodeIgniter?


Please follow the steps below:

  1. Open file manager in cPanel, and go to the installation folder.
  2. Go inside the libraries folder under the application folder.
  3. Create a simple library file, Test function.
  4. Define a class, with the same name, as the file name.
  5. Inside the class, create a simple function, which will return the current date.
  6. To make the library function available globally, you need to add the file name to the autoload.php file.
  7. Now create a Controller file, Test.
  8. Write a simple function, to save the output from the library function, and pass it to the loaded view file called test.
  9. Create a view file as test, to display the output of the test controller.
  10. Now, check the output.
  11. This will display the current date.
  12. Now, write the library function directly to the view file.
  13. The same output is displayed.
  14. Delete the library function, and the variable from the controller function.
  15. The output is still the same.
  16. This shows that the library functions can be used in view files directly, instead of passing a value through the controller.
  17. This way you can create and use library functions in PHP CodeIgniter.