|
1 | | -# codeigniter-psr4-autoloader |
2 | | -CodeIgniter 3 PSR-4 Autoloader Support |
| 1 | +<p align="center"> |
| 2 | + <a href="https://codeigniter.com/" target="_blank"> |
| 3 | + <img src="https://codeigniter.com/assets/images/ci-logo-big.png" height="100px"> |
| 4 | + </a> |
| 5 | + <h1 align="center">CodeIgniter PSR-4 Autoload</h1> |
| 6 | + <br> |
| 7 | +</p> |
| 8 | + |
| 9 | +CodeIgniter 3 PSR-4 Autoloader for Application |
| 10 | + |
| 11 | +[](https://packagist.org/packages/yidas/codeigniter-psr4-autoload) |
| 12 | +[](https://packagist.org/packages/yidas/codeigniter-psr4-autoload) |
| 13 | +[](https://packagist.org/packages/yidas/codeigniter-psr4-autoload) |
| 14 | + |
| 15 | +FEATURES |
| 16 | +-------- |
| 17 | + |
| 18 | +- ***PSR-4 Namespace** support as Yii2 & Laravel elegant patterns like* |
| 19 | + |
| 20 | +- *Easy way to use **Interface, Trait, Abstract and Extending Class*** |
| 21 | + |
| 22 | +- ***Whole Codeigniter application** directory structure support* |
| 23 | + |
| 24 | + |
| 25 | +DEMONSTRATION |
| 26 | +------------- |
| 27 | + |
| 28 | +Autoload class files by PSR-4 namespace with `app` prefix in Codeigniter: |
| 29 | + |
| 30 | +```php |
| 31 | +# /application/libraries/MemberService.php: |
| 32 | +\app\libraries\MemberService::auth(); |
| 33 | + |
| 34 | +# /application/widgets/StatWidget.php: |
| 35 | +\app\widgets\StatWidget::run(); |
| 36 | + |
| 37 | +class Blog_model extends app\models\BaseModel {} |
| 38 | +class Blog extends app\libraries\BaseController {} |
| 39 | +class Car implements app\contracts\CarInterface {} |
| 40 | +``` |
| 41 | + |
| 42 | +More specifically, create a class with namespace refering to the file path `\application\helpers\`: |
| 43 | + |
| 44 | +```php |
| 45 | +<?php |
| 46 | +namespace app\helpers; |
| 47 | + |
| 48 | +class ArrayHelper |
| 49 | +{ |
| 50 | + public static function indexBy($input) {} |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +Then call it in Controller action: |
| 55 | + |
| 56 | +```php |
| 57 | +<?php |
| 58 | +use app\helpers\ArrayHelper; |
| 59 | +... |
| 60 | +ArrayHelper::indexBy($input); |
| 61 | +\app\helpers\ArrayHelper::indexBy($input); |
| 62 | +``` |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +REQUIREMENTS |
| 67 | +------------ |
| 68 | + |
| 69 | +This library requires the following: |
| 70 | + |
| 71 | +- PHP 5.3.0+ |
| 72 | +- CodeIgniter 3.0.0+ |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +INSTALLATION |
| 77 | +------------ |
| 78 | + |
| 79 | +Run Composer in your Codeigniter project under the folder `\application`: |
| 80 | + |
| 81 | + composer require yidas/codeigniter-psr4-autoload |
| 82 | + |
| 83 | +Check Codeigniter `application/config/config.php`: |
| 84 | + |
| 85 | +```php |
| 86 | +$config['composer_autoload'] = TRUE; |
| 87 | +``` |
| 88 | + |
| 89 | +> You could customize the vendor path into `$config['composer_autoload']` |
| 90 | +
|
| 91 | +--- |
| 92 | + |
| 93 | +CONFIGURATION |
| 94 | +------------- |
| 95 | + |
| 96 | +After installation, you need to register it into Codeigniter system hook. |
| 97 | + |
| 98 | +### 1. Enabling Hooks |
| 99 | + |
| 100 | +The hooks feature can be globally enabled/disabled by setting the following item in the `application/config/config.php` file: |
| 101 | + |
| 102 | +```php |
| 103 | +$config['enable_hooks'] = TRUE; |
| 104 | +``` |
| 105 | + |
| 106 | +### 2. Adding a Hook |
| 107 | + |
| 108 | +Hooks are defined in the `application/config/hooks.php` file, add above hook into it: |
| 109 | + |
| 110 | +```php |
| 111 | +/* |
| 112 | + | ------------------------------------------------------------------- |
| 113 | + | Auto-load All Classes with PSR-4 |
| 114 | + | ------------------------------------------------------------------- |
| 115 | + | After registering \yidas\Psr4Autoload, you could auto-load every |
| 116 | + | classes in the whole Codeigniter application with `app` PSR-4 |
| 117 | + | prefix by default, for example: |
| 118 | + | # /application/libraries/MemberService.php: |
| 119 | + | \app\libraries\MemberService::auth(); |
| 120 | + | # /application/widgets/StatWidget.php: |
| 121 | + | \app\widgets\StatWidget::run(); |
| 122 | + | class Blog_model extends app\models\BaseModel {} |
| 123 | + | class Blog extends app\libraries\BaseController {} |
| 124 | + | class Car_model implements app\contracts\CarInterface {} |
| 125 | + | |
| 126 | + | The called class need to define namespace to support PSR-4 Autoload |
| 127 | + | only, which means it would not support CI_Loader anymore. |
| 128 | + | |
| 129 | + | @see https://github.com/yidas/codeigniter-psr4-autoload |
| 130 | + */ |
| 131 | + |
| 132 | +$hook['pre_system'][] = [new yidas\Psr4Autoload, 'register']; |
| 133 | +``` |
| 134 | + |
| 135 | +--- |
| 136 | + |
| 137 | +USAGE |
| 138 | +----- |
| 139 | + |
| 140 | +After installation, the namespace prefix `app` is used for the current Codeigniter application directory. |
| 141 | + |
| 142 | +### Extending Class |
| 143 | + |
| 144 | +Create a class with PSR-4 namespace under `application` directory, for eaxmple `application/model/BaseModel.php`: |
| 145 | + |
| 146 | +```php |
| 147 | +<?php |
| 148 | +namespace app\models; |
| 149 | + |
| 150 | +class BaseModel extends \CI_Model {} |
| 151 | +``` |
| 152 | + |
| 153 | +Then define a class to extend above class, for eaxmple `application/model/My_model.php`: |
| 154 | + |
| 155 | +```php |
| 156 | +<?php |
| 157 | + |
| 158 | +class My_model extends app\models\BaseModel {} |
| 159 | +``` |
| 160 | + |
| 161 | +> In this case, Codeigniter `My_model` could not use PSR-4 namespace. |
| 162 | +
|
| 163 | + |
| 164 | +### Interface |
| 165 | + |
| 166 | +Create a interface under `application` directory, for eaxmple `application/interface/CarInterface.php`: |
| 167 | + |
| 168 | +```php |
| 169 | +<?php |
| 170 | +namespace app\interfaces; |
| 171 | + |
| 172 | +interface CarInterface {} |
| 173 | +``` |
| 174 | + |
| 175 | +Then apply the interface to a class, for eaxmple `application/libraries/Car.php`: |
| 176 | + |
| 177 | +```php |
| 178 | +<?php |
| 179 | +namespace app\libraries; |
| 180 | + |
| 181 | +class Car implements \app\interfaces\CarInterface {} |
| 182 | +``` |
| 183 | + |
| 184 | +> In this case, the `Car` lib could be called by using `new \app\libraries\Car;`. |
| 185 | +
|
| 186 | + |
| 187 | +### Trait |
| 188 | + |
| 189 | +Create a trait under `application` directory, for eaxmple `application/libraries/LogTrait.php`: |
| 190 | + |
| 191 | +```php |
| 192 | +<?php |
| 193 | +namespace app\libraries; |
| 194 | + |
| 195 | +trait LogTrait {} |
| 196 | +``` |
| 197 | + |
| 198 | +Then inject the trait into a class, for eaxmple `application/controller/Blog.php`: |
| 199 | + |
| 200 | +```php |
| 201 | +class Blog extends CI_Controller |
| 202 | +{ |
| 203 | + use \app\libraries\LogTrait; |
| 204 | +} |
| 205 | +``` |
| 206 | + |
| 207 | +### Abstract |
| 208 | + |
| 209 | +Create an abstract under `application` directory, for eaxmple `application/libraries/BaseController.php`: |
| 210 | + |
| 211 | +```php |
| 212 | +<?php |
| 213 | +namespace app\libraries; |
| 214 | + |
| 215 | +abstract class BaseController extends \CI_Controller {} |
| 216 | +``` |
| 217 | + |
| 218 | +Then define a class to extend above abstract class, for eaxmple `application/libraries/BaseController.php`: |
| 219 | + |
| 220 | +```php |
| 221 | +class Blog extends app\libraries\BaseController {} |
| 222 | +``` |
| 223 | + |
| 224 | +--- |
| 225 | + |
| 226 | +CONCEPTION |
| 227 | +---------- |
| 228 | + |
| 229 | +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. |
| 230 | + |
| 231 | +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. |
| 232 | + |
| 233 | +--- |
| 234 | + |
| 235 | +LIMITATIONS |
| 236 | +----------- |
| 237 | + |
| 238 | +### Namespace Class No Longer Support CI_Loader |
| 239 | + |
| 240 | +The called class need to define namespace to support PSR-4 Autoload only, which means it would not support Built-in CI_Loader anymore. |
0 commit comments