CodeIgniter Training 5th part: Libraries and helpers

Created by Eric Murillo on 2019-06-15

4z.com

 

 

Table of Contents

Presentation. 1

Library vs helper. 1

Creating libraries. 1

Sending parameters to the library. 1

Accessing the CodeIgniter objects. 1

Helpers. 1

Validation. 1

Bibliography. 1

End of document. 1

 

Presentation

In the last training we used the validation library and the assistant (or helper) of CodeIgniter forms. Both are available in any framework installation, but in this training, we will learn to create our own helpers and libraries. Before continuing, make sure you have completed and understood the previous training.

Library vs helper

What is the difference between a library and a helper? A library is usually a class that provides a series of functionalities, and these functionalities share information. For example, a library that allows access to the Twitter API needs its methods to share data such as the password and the username, so the ideal is to have a class that allows you to save that information. On the other hand, a helper can be understood as a group of functions that, although they share a field of use, are independent of each other. A good example of this would be the CodeIgniter forms helper.

Creating libraries

The first step will be to create a file 'My_library.php' in 'application/libraries’ and copy the following code. For now, there is only one function that returns the string "Hello World", but it will be enough to learn how to load it and use its methods:

CodeIgniter indicates the following guidelines when naming the libraries:

·         The name of the file must start with a capital letter.

·         The name of the class must start with a capital letter.

·         The names of the class and the file must match.

Then we will go to the driver 'employees.php' that we already had and add the following function:

It is important to highlight that, to be able to call our helloWorld() function, we must use the name of our library in lowercase, if we had done:

We would have received an error indicating that the object 'My_library' does not exist. On the other hand, when loading the library, CodeIgniter does not differentiate if the first letter is capitalized or not. Now we can check if it works:

Sending parameters to the library

It is very likely that we need to send parameters to the library at the time of loading, for this, the function 'load-> library()' allows you to pass a second argument composed of an associative array. In order to receive this data, we will need to create a constructor in the library that receives this array:

Now our library will return the text that has been passed at the time of loading, and in our controller, we will have to call it as follows:

Accessing the CodeIgniter objects

It is possible to access the CI resources from our library, even if this stops it from being completely independent of the framework. For this, it is necessary to obtain the CodeIgniter object:

VERY important to pass the function 'get_instance()' by reference, in this way, the existing instance of CodeIgniter will be copied instead of creating a new one.

Once this is done, we can call the CI functions as we do in the controllers or models, but replacing the '$this' with '$CI':

Helpers

To create a helper, we must create a file in 'application/helpers' (for our example called 'my_helper.php') and write in our functions:

We can use it in the following way:

The difference with the libraries is that we do not need to call the helper through the object '$this'. The helper working:

Validation

At the end of the training you should submit a report with the steps to follow to use a library or an assistant. You should indicate in which folder these features are stored and include screenshots of the code and views.

 

After this training you should be able to understand the basics of the framework on which 4z websites are based. I hope it helped you.

 

Bibliography

https://docs.switzernet.com/3/public/190611-CodeIgniter1/

https://docs.switzernet.com/3/public/190612-CodeIgniter2/

https://docs.switzernet.com/3/public/190613-CodeIgniter3/

https://docs.switzernet.com/3/public/190614-CodeIgniter4/

https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mariadb-php-lamp-stack-debian9

https://www.apachefriends.org/index.html

https://www.codeigniter.com/

End of document

 

 

*   *   *

© 4z.com