, (*1)
CodeIgniter PSR-4 Autoload
CodeIgniter 3 PSR-4 Autoloader for Application, (*2)
, (*3)
This PSR-4 extension is collected into yidas/codeigniter-pack which is a complete solution for Codeigniter framework., (*4)
FEATURES
-
PSR-4 Namespace support as Yii2 & Laravel elegant patterns like, (*5)
-
Easy way to use Interface, Trait, Abstract and Extending Class, (*6)
-
Whole Codeigniter application directory structure support, (*7)
OUTLINE
DEMONSTRATION
By default, all PSR-4 namespace with app
prefix in Codeigniter would relates to application directory., (*8)
- The class
/application/libraries/MemberService.php
could be called by:
new \app\libraries\MemberService;
- The class
/application/services/Process.php
with static run()
method could be called by:
\app\services\Process::run();
- Enable to extend or implement classes with standard way:
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:, (*9)
- PHP 5.3.0+
- CodeIgniter 3.0.0+
INSTALLATION
Run Composer in your Codeigniter project under the folder \application
:, (*10)
composer require yidas/codeigniter-psr4-autoload
Check Codeigniter application/config/config.php
:, (*11)
$config['composer_autoload'] = TRUE;
You could customize the vendor path into $config['composer_autoload']
, (*12)
USAGE
After installation, the namespace prefix app
is used for the current Codeigniter application directory., (*13)
Static Class
Create a hepler with PSR-4 namespace with a new helpers
folder under application
directory, for eaxmple \application\helpers\
:, (*14)
<?php
namespace app\helpers;
class ArrayHelper
{
public static function indexBy($input) {}
}
Then call it in Controller action:, (*15)
<?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
:, (*16)
<?php
namespace app\models;
class BaseModel extends \CI_Model {}
Then define a class to extend above class, for eaxmple application/model/My_model.php
:, (*17)
<?php
class My_model extends app\models\BaseModel {}
In this case, Codeigniter My_model
could not use PSR-4 namespace., (*18)
Interface
Create a interface with a new interface
folder under application
directory, for eaxmple application/interface/CarInterface.php
:, (*19)
<?php
namespace app\interfaces;
interface CarInterface {}
Then apply the interface to a class, for eaxmple application/libraries/Car.php
:, (*20)
<?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;
., (*21)
Trait
Create a trait under application
directory, for eaxmple application/libraries/LogTrait.php
:, (*22)
<?php
namespace app\components;
trait LogTrait {}
Then inject the trait into a class, for eaxmple application/controller/Blog.php
:, (*23)
class Blog extends CI_Controller
{
use \app\components\LogTrait;
}
Abstract
Create an abstract under application
directory, for eaxmple application/libraries/BaseController.php
:, (*24)
<?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
:, (*25)
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., (*26)
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., (*27)
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., (*28)