Creating a Library - Part 3


Posted by Tuts Admin on 2018-07-30

Contributions

 

Want to be a contributing author and help grow the CodeIgniter community?

Register and account and use the contact form and drop us a note.

Donations

 

We appreciate any donation you're willing to send.

Visit the donations page

Donations collected are used to pay for hosting, domain names, further development of the website, and gives me a little more time to write tutorials.

Subscribe

Get an email when new content is added.


0 v4-dev Libraries Beginner

Creating a Library - Part 3

Loading and using your library.

Notice: CodeIgniter 4 is currently in development as of the writing of this article. This article is aimed at beginners.

In part 1 we learned how to create our spiffy new library. In part 2 we learned how to connect and use some of CodeIgniter4's core features, such as helpers and the database driver.

In this article, we'll learn how to load, instantiate and use our spiffy new library. We'll be using a Controller here, but with some slight modification, it can be used by Models, other libraries, etc.

Our Controller:

We'll assume we have a file in application/Controllers/Mycontroller.php.

<?php namespace App\Controllers;

    use CodeIgniter\Controller;
    use App\Libraries\MyLib;

    class Mycontroller extends Controller {

        public function index()
        {
          echo 'Hello World';
        }

    }

Now we've set up the basics of our controller.

  • We've set our controller to the App\Controllers namespace.
  • We've used (loaded) the CodeIgniter\Controller namespace/file
  • We've used (loaded) our App\Libraries\MyLib namespace/file
  • We've extended the Controller with Mycontroller
  • and we created the default index() method

Some of this will look familiar to Codeigniter 3 developers as we extend the CI_Controller in CI3, it's the same thing, just a little different setup. There's really no change when creating methods.

Now we'll use our library in some meaningful way.

So let's change our index() function in our controller to the below:

public function index()
{
    // get an instance of our MyLib library
    $MyLib = new MyLib();

    // call and run the getAll() method
    $result = $MyLib->getAll();

    //... do something with the result
    print_r($result);
}

That's it. For another example we can use the getOne() method, and even make it dynamic.

In our controller, let's create a new method.

public function method2($id)
{
    // get an instance of our MyLib library
    $MyLib = new MyLib();

    // call and run the getOne() method
    $result = $MyLib->getOne($id);

    //... do something with the result
    print_r($result);
}

Now when we call http://example.com/myconntroller/method2/2341 the same routing magic happens inside CodeIgniter and calls the Mycontroller{} controller class, the method2() method and passes $id = 2341 as a variable to the method.

In future articles, we'll begin using and creating libraries in a much more OOP respective manner. Hopefully, these three articles have given you a good idea of some of the differences between CodeIgnoter 3 and CodeIgniter 4 and how to begin using them.

Until Next time...