99CodeIgniter 3 PSR-4 Autoloader for Application
1010
1111[ ![ Latest Stable Version] ( https://poser.pugx.org/yidas/codeigniter-psr4-autoload/v/stable?format=flat-square )] ( https://packagist.org/packages/yidas/codeigniter-psr4-autoload )
12- [ ![ Latest Unstable Version] ( https://poser.pugx.org/yidas/codeigniter-psr4-autoload/v/unstable?format=flat-square )] ( https://packagist.org/packages/yidas/codeigniter-psr4-autoload )
1312[ ![ License] ( https://poser.pugx.org/yidas/codeigniter-psr4-autoload/license?format=flat-square )] ( https://packagist.org/packages/yidas/codeigniter-psr4-autoload )
1413
14+ This PSR-4 extension is collected into [ yidas/codeigniter-pack] ( https://github.com/yidas/codeigniter-pack ) which is a complete solution for Codeigniter framework.
15+
1516FEATURES
1617--------
1718
@@ -21,44 +22,47 @@ FEATURES
2122
2223- *** Whole Codeigniter application** directory structure support*
2324
25+ ---
26+
27+ OUTLINE
28+ -------
29+
30+ - [ Demonstration] ( #demonstration )
31+ - [ Requirements] ( #requirements )
32+ - [ Installation] ( #installation )
33+ - [ Usage] ( #usage )
34+ - [ Extending Class] ( #extending-class )
35+ - [ Interface] ( #interface )
36+ - [ Trait] ( #trait )
37+ - [ Abstract] ( #abstract )
38+ - [ Conception] ( #conception )
39+ - [ Limitations] ( #limitations )
40+
41+ ---
2442
2543DEMONSTRATION
2644-------------
2745
28- Autoload class files by PSR-4 namespace with ` app ` prefix in Codeigniter:
46+ By default, all PSR-4 namespace with ` app ` prefix in Codeigniter would relates to application directory.
2947
30- ``` php
31- # /application/libraries/MemberService.php:
32- \app\libraries\MemberService::auth();
33-
34- # /application/widgets/StatWidget.php:
35- \app\widgets\StatWidget::run();
48+ - The class ` /application/libraries/MemberService.php ` could be called by:
3649
37- class Blog_model extends app\models\BaseModel {}
38- class Blog extends app\libraries\BaseController {}
39- class Car implements app\contracts\CarInterface {}
50+ ``` php
51+ new \app\libraries\MemberService;
4052```
4153
42- More specifically, create a class with namespace refering to the file path ` \application\helpers\ ` :
54+ - The class ` /application/services/Process.php ` with ` static run() ` method could be called by :
4355
4456``` php
45- <?php
46- namespace app\helpers;
47-
48- class ArrayHelper
49- {
50- public static function indexBy($input) {}
51- }
57+ \app\services\Process::run();
5258```
5359
54- Then call it in Controller action :
60+ - Enable to extend or implement classes with standard way :
5561
5662``` php
57- <?php
58- use app\helpers\ArrayHelper;
59- ...
60- ArrayHelper::indexBy($input);
61- \app\helpers\ArrayHelper::indexBy($input);
63+ class Blog_model extends app\models\BaseModel {}
64+ class Blog extends app\components\BaseController {}
65+ class Car implements app\contracts\CarInterface {}
6266```
6367
6468---
@@ -90,54 +94,36 @@ $config['composer_autoload'] = TRUE;
9094
9195---
9296
93- CONFIGURATION
94- -------------
95-
96- After installation, you need to register it into Codeigniter system hook.
97+ USAGE
98+ -----
9799
98- ### 1. Enabling Hooks
100+ After installation, the namespace prefix ` app ` is used for the current Codeigniter application directory.
99101
100- The hooks feature can be globally enabled/disabled by setting the following item in the ` application/config/config.php ` file:
101102
102- ``` php
103- $config['enable_hooks'] = TRUE;
104- ```
103+ ### Static Class
105104
106- ### 2. Adding a Hook
107105
108- Hooks are defined in the ` application/config/hooks.php ` file, add above hook into it :
106+ Create a hepler with PSR-4 namespace with a new ` helpers ` folder under ` application ` directory, for eaxmple ` \application\helpers\ ` :
109107
110108``` 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- ```
109+ <?php
110+ namespace app\helpers;
134111
135- ---
112+ class ArrayHelper
113+ {
114+ public static function indexBy($input) {}
115+ }
116+ ```
136117
137- USAGE
138- -----
118+ Then call it in Controller action:
139119
140- After installation, the namespace prefix ` app ` is used for the current Codeigniter application directory.
120+ ``` php
121+ <?php
122+ use app\helpers\ArrayHelper;
123+ ...
124+ ArrayHelper::indexBy($input);
125+ \app\helpers\ArrayHelper::indexBy($input);
126+ ```
141127
142128### Extending Class
143129
@@ -163,7 +149,7 @@ class My_model extends app\models\BaseModel {}
163149
164150### Interface
165151
166- Create a interface under ` application ` directory, for eaxmple ` application/interface/CarInterface.php ` :
152+ Create a interface with a new ` interface ` folder under ` application ` directory, for eaxmple ` application/interface/CarInterface.php ` :
167153
168154``` php
169155<?php
@@ -190,7 +176,7 @@ Create a trait under `application` directory, for eaxmple `application/libraries
190176
191177``` php
192178<?php
193- namespace app\libraries ;
179+ namespace app\components ;
194180
195181trait LogTrait {}
196182```
@@ -200,7 +186,7 @@ Then inject the trait into a class, for eaxmple `application/controller/Blog.php
200186``` php
201187class Blog extends CI_Controller
202188{
203- use \app\libraries \LogTrait;
189+ use \app\components \LogTrait;
204190}
205191```
206192
@@ -210,15 +196,15 @@ Create an abstract under `application` directory, for eaxmple `application/libra
210196
211197``` php
212198<?php
213- namespace app\libraries ;
199+ namespace app\components ;
214200
215201abstract class BaseController extends \CI_Controller {}
216202```
217203
218204Then define a class to extend above abstract class, for eaxmple ` application/libraries/BaseController.php ` :
219205
220206``` php
221- class Blog extends app\libraries \BaseController {}
207+ class Blog extends app\components \BaseController {}
222208```
223209
224210---
0 commit comments