DEV Community

Francis Ilechukwu
Francis Ilechukwu

Posted on • Updated on

A Package Manager for Code Igniter 3

Code Igniter is a simple and yet powerful PHP MVC Framework. With it, one can build rich featured web applications in almost half the time required to write the same application in plain PHP while taking care of security issues at the same time.

I have used Code Igniter for about 3 years to know that it’s core is really efficient and is one of the most simplest MVC framework out there.

As small and simple as it is, its applications ranges from simple to very complex projects.

There are only two reasons I can think of so far, that the framework has lacked over the years which prompted people to choose its alternative (Laravel) to it.

The first reason is its failure to adapt to the new PSR-4 Auto-Loading feature that PHP rolled out. You’ll notice this when you try to load the same class name but in different directories as Code Igniter 3 was built to adapt to PHP 5.3.+ and has a built-in preventive mechanism to avoid clashing of class names.

The code in the controller below will load only the library assigned to alias1

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MainController extends CI_Controller {

  function index() {
    $this->load->library("nodes/Lib", null, "alias1");
    $this->load->library("Lib", null, "alias2");
  }
}
?>

This problem is currently being solved by Code Igniter 4 which is currently in Beta.

The second reason is, unlike Laravel, Code Igniter does not have a repository for packages that can save you a lot of development time like Packagist.

you may get libraries from GitHub but you still need to modify/copy them to the right directory in the right structure by yourself to suit Code Igniter before you can use them.

Some of the libraries you may find, implement functionalities that already reside within Code Igniter, hence re-inventing the wheel. You won’t want to do this if you are the one writing a library unless it’s necessary.

Splint Logo

Splint is a Package and Dependency manager specifically built for Code Igniter to allow one build mini applications, packages that can make use of the Code Igniter Loader to load resources such as views, models, configs, libraries, etc. Whatever you can do within Code Igniter applications, you can do also within Splint packages.

Loading and using a library within a Splint package is as easy as shown below.

<?php
$this->load->splint("francis94c/ci-preference", "+CIPreferences", null, "prefs");
$this->prefs->get("key");
?>

As you can see, the splint() function isn’t by default, a member of the loader class. However, you will need to modify the Loader class from the application/core folder. The Splint Terminal tool will do this automatically for you whenever you install a package.

Splint has a Wiki where you can learn all about how to build Splint packages. However, Splint is still very much new and has very few packages you can make use of. You can always go to their website to see the packages they have that you can install.

Installing a package using the Splint Terminal tool is as easy ascan be with the following command.

splint install vendor/package_name

Splint also has an SDK which is in fact, a complete Code Igniter distribution with its loader already modified along with one platform package for use when developing/testing a package.

All packages must be publicly hosted on GitHub before you can import them to Splint.

A Sample Splint Package

Let’s take a look at the Splint package I built for persistence of simple key-value pairs. I call the package ci-preference and is hosted on GitHub. You can visit it’s Splint page here.

This library makes use of two main functions which are set() and get() to set values and get values by key.

All you have to do to use this library in Code Igniter is to download the Splint terminal tool and run splint install francis94c/ci-preference to have the package installed within your Code Igniter distribution.

Then within any controller/model, use the package as described in its README which you would find either on the Splint page for the package or on it’s GitHub page.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class A_Controller extends CI_Controller {

  function index() {
    $params = array();
    $params["file_name"] = "preferences"; // File name of the preference file.
    $this->load->splint("francis94c/ci-preference", "+CIPreferences", $params, "alias");
    $this->alias->get("key", "defaultVal");
    $this->alias->set("key", "val");
    $this->alias->increment("key1", 5) // Increments 'key1' by 5.
    $this->alias->increment("key1") // increments 'key1' by 1.
    $this->alias->concat("key2", "A") // Concatenates 'A' to 'key2'.
    $this->alias->commit(); // Commits all values to file.
  }
}
?>

That’s all about it. If you have ideas about libraries you would like to build and use within Code Igniter, have a look at the Developer Docs for Splint.

You can also modify other libraries/packages out there to suit Code Igniter and publish/import it to Splint as that will surely, go a long way in helping the Code Igniter community.

Top comments (0)