Press ESC to close

Manoj Bist

Using Models in Codeigniter Library

Using Models in Codeigniter 4 Library

Codeigniter, a framework that is easy to understand and easy to use. Codeigniter provides a well documented easy to understand user guide with it. You can find an online user guide or offline user guide. To get an offline user guide you have to download the Codeigniter framework from its official website Codeigniter.com.
Today I am going share, how to use a model in Codeigniter library files. For this example, I assume you already have downloaded Codeigniter 4 and configured it in your localhost.
Previously we were able to call a model in the library by using Codeigniter Instance 
$CI = & get_instance();

$CI->load->model('your_model');

 $CI->your_model->your_method()

But now, with Codeigniter 4, there is a more convenient way to use models in Codeigniter library files.
Codeigniter 4 has now support for PSR-4. 
So we have a more easy way to use models in Codeigniter.
Here is how you can use models in the Codeigniter library.
Let’s assume, we have a helper file Productblocks.php  and a model ModelProduct.php
Now I am going to use the model in the library
$modelProduct = new AppModelsModelProduct;
let assume, we have a method getProduct() in our ModelProduct.php file
$modelProduct->getProduct();
How easy is that, we do not need to get an instance of Codeigniter just use Namespaces.
I am glad, you read it all.
Thanks, Happy Coding

Manoj Bist

A Passionate Web Developer/Designer With over 6 years of experience in the industry, Manoj Bist is a seasoned professional who combines technical expertise with a down-to-earth demeanor. As a lover of both technology and design, he thrives on creating seamless digital experiences that captivate and inspire.

Leave a Reply

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