yidas/codeigniter-psr4-autoload
Composer 安装命令:
composer require yidas/codeigniter-psr4-autoload
包简介
CodeIgniter 3 PSR-4 Autoloader for Application
README 文档
README
CodeIgniter PSR-4 Autoload
CodeIgniter 3 PSR-4 Autoloader for Application
This PSR-4 extension is collected into yidas/codeigniter-pack which is a complete solution for Codeigniter framework.
FEATURES
-
PSR-4 Namespace support as Yii2 & Laravel elegant patterns like
-
Easy way to use Interface, Trait, Abstract and Extending Class
-
Whole Codeigniter application directory structure support
OUTLINE
DEMONSTRATION
By default, all PSR-4 namespace with app prefix in Codeigniter would relates to application directory.
- The class
/application/libraries/MemberService.phpcould be called by:
new \app\libraries\MemberService;
- The class
/application/services/Process.phpwithstatic 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:
- PHP 5.3.0+
- CodeIgniter 3.0.0+
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_modelcould 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
Carlib could be called by usingnew \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.
yidas/codeigniter-psr4-autoload 适用场景与选型建议
yidas/codeigniter-psr4-autoload 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.54k 次下载、GitHub Stars 达 54, 最近一次更新时间为 2018 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「autoloader」 「autoload」 「codeigniter」 「PSR-4」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yidas/codeigniter-psr4-autoload 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yidas/codeigniter-psr4-autoload 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yidas/codeigniter-psr4-autoload 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Environment processor and contexts autoloader
The CodeIgniter Redis package
An package to make it easy to autoload code.
Tune your autoloader
Extension to autoload modules dynamically
统计信息
- 总下载量: 20.54k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 55
- 点击次数: 25
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-01-16