Home

Awesome

<p align="center"> <a href="https://codeigniter.com/" target="_blank"> <img src="https://camo.githubusercontent.com/b1df5003f5bc21277aeb87c5d3558242b74eb54a61a253cc061a38c7a43bcac7/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f7a682f372f37632f436f646549676e697465722e706e67"> </a> <h1 align="center">CodeIgniter PSR-4 Autoload</h1> <br> </p>

CodeIgniter 3 PSR-4 Autoloader for Application

Latest Stable Version License Total Downloads Monthly Downloads

This PSR-4 extension is collected into yidas/codeigniter-pack which is a complete solution for Codeigniter framework.

FEATURES


OUTLINE


DEMONSTRATION

By default, all PSR-4 namespace with app prefix in Codeigniter would relates to application directory.

new \app\libraries\MemberService;
\app\services\Process::run();
class Blog_model extends app\models\BaseModel {}
class Blog extends app\components\BaseController {}
class Car implements app\contracts\CarInterface {}

REQUIREMENTS

This library requires the following:


INSTALLATION

Run Composer in your Codeigniter project under the folder \application:

composer require yidas/codeigniter-psr4-autoload

Check Codeigniter application/config/config.php:

$config['composer_autoload'] = TRUE;

You could customize the vendor path into $config['composer_autoload']


USAGE

After installation, the namespace prefix app is used for the current Codeigniter application directory.

Static Class

Create a hepler with PSR-4 namespace with a new helpers folder under application directory, for eaxmple \application\helpers\:

<?php
namespace app\helpers;

class ArrayHelper
{
    public static function indexBy($input) {}
}

Then call it in Controller action:

<?php
use app\helpers\ArrayHelper;
...
ArrayHelper::indexBy($input);
\app\helpers\ArrayHelper::indexBy($input);

Extending Class

Create a class with PSR-4 namespace under application directory, for eaxmple application/model/BaseModel.php:

<?php
namespace app\models;

class BaseModel extends \CI_Model {}

Then define a class to extend above class, for eaxmple application/model/My_model.php:

<?php

class My_model extends app\models\BaseModel {}

In this case, Codeigniter My_model could not use PSR-4 namespace.

Interface

Create a interface with a new interface folder under application directory, for eaxmple application/interface/CarInterface.php:

<?php
namespace app\interfaces;

interface CarInterface {}

Then apply the interface to a class, for eaxmple application/libraries/Car.php:

<?php
namespace app\libraries;

class Car implements \app\interfaces\CarInterface {}

In this case, the Car lib could be called by using new \app\libraries\Car;.

Trait

Create a trait under application directory, for eaxmple application/libraries/LogTrait.php:

<?php
namespace app\components;

trait LogTrait {}

Then inject the trait into a class, for eaxmple application/controller/Blog.php:

class Blog extends CI_Controller
{
    use \app\components\LogTrait;
}

Abstract

Create an abstract under application directory, for eaxmple application/libraries/BaseController.php:

<?php
namespace app\components;

abstract class BaseController extends \CI_Controller {}

Then define a class to extend above abstract class, for eaxmple application/libraries/BaseController.php:

class Blog extends app\components\BaseController {}

CONCEPTION

Codeigniter Loader is a good practice for loading one time instantiated component just like Yii2 Application Components, but it's lacking of Class mapping, which makes inconvenience to load classes including interfaces, traits, abstracts or extending classes.

Thus, You could defind classes with PSR-4 Namespace while these classes are not component kind, even Helpers which you may want use static method and customized class name.


LIMITATIONS

Namespace Class No Longer Support CI_Loader

The called class need to define namespace to support PSR-4 Autoload only, which means it would not support Built-in CI_Loader anymore.