Blogger Information
Blog 5
fans 0
comment 0
visits 6266
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
CodeIgniter3利用composer实现命名空间类的自动加载
づ奈何ā
Original
1587 people have browsed it

安装composer

这里只简单介绍一下windows下通过composer安装程序安装。也许以后会写一篇文章来总结composer的安装和使用
下载Composer-Setup.exe,然后按照提示一步步的安装就行了。可参考Download Composer
检测composer是否已经安装成功:新建一个命令行窗口,然后执行命令composer -V,如果能正确输出composer的版本,那就说明安装成功了。

在CodeIgniter中启用composer自动加载器

CodeIgniter安装配置是按照CI官方文档的安装说明配置的,且没有进行个性化设置,即没有设置$system_path、$application_folder和$view_folder。

用composer安装依赖

在命令行中切换到CI的根目录,即composer.json所在的目录,执行命令
composer install
执行完成后,会出现vendor目录,vendor目录下有文件autoload.php。

修改composer配置文件

修改文件application/config/config.php中的配置项$config[‘composer_autoload’]。修改它的值为上述autoload.php的路径(相对于index.php的路径),例如:
$config['composer_autoload'] = 'vendor/autoload.php';

测试composer自动加载器

在CI根目录执行命令
composer require smarty/smarty
Smarty是个php的模板引擎,这里仅用作测试。
修改文件application/controllers/Welcome.php,修改index方法如下:

  1. public function index(){
  2. $smarty = new Smarty();
  3. var_dump($smarty);
  4. }

如果没有提示找不到类Smarty,并且能正常输出$smarty的值(不是NULL)时,说明自动加载器已经正确启用了。

自定义带有命名空间的类

创建Base类

在application/controllers目录中创建文件Base.php。这是个自定义类,用来扩展CI_Controllers的类。顶级命名空间为app\,它对应的目录为application/。于是命名空间app\controlles\对应目录application/controllers/。(这应该是符合PSR-4的吧)内容如下

  1. <?php
  2. namespace app\controllers;
  3. use CI_Controller;
  4. defined('BASEPATH') OR exit('No direct script access allowed');
  5. class Base extends CI_Controller{
  6. public function index(){
  7. echo 'This is a msg from class Base.';
  8. }
  9. }

创建Demo类

在application/controllers目录中创建文件Demo.php。这个类用来作为CI中的控制器,要在浏览器中访问,因此不能给它设置命名空间,即不不能包含namaspace语句。其内容如下

  1. <?php
  2. use app\controllers\Base;
  3. class Demo extends Base{
  4. }

在composer配置文件中加入命名空间的配置

修改CI根目录中的composer.json。

为方便对比,防止改错,假设修改前的文件内容为

  1. {
  2. "description": "The CodeIgniter framework",
  3. "name": "codeigniter/framework",
  4. "type": "project",
  5. "homepage": "https://codeigniter.com",
  6. "license": "MIT",
  7. "support": {
  8. "forum": "http://forum.codeigniter.com/",
  9. "wiki": "https://github.com/bcit-ci/CodeIgniter/wiki",
  10. "irc": "irc://irc.freenode.net/codeigniter",
  11. "source": "https://github.com/bcit-ci/CodeIgniter"
  12. },
  13. "require": {
  14. "php": ">=5.2.4",
  15. "smarty/smarty": "^3.1"
  16. },
  17. "suggest": {
  18. "paragonie/random_compat": "Provides better randomness in PHP 5.x"
  19. },
  20. "require-dev": {
  21. "mikey179/vfsStream": "1.1.*",
  22. "phpunit/phpunit": "4.* || 5.*"
  23. }
  24. }

修改后的文件内容(注意在前一项末尾加个英文逗号)

  1. {
  2. "description": "The CodeIgniter framework",
  3. "name": "codeigniter/framework",
  4. "type": "project",
  5. "homepage": "https://codeigniter.com",
  6. "license": "MIT",
  7. "support": {
  8. "forum": "http://forum.codeigniter.com/",
  9. "wiki": "https://github.com/bcit-ci/CodeIgniter/wiki",
  10. "irc": "irc://irc.freenode.net/codeigniter",
  11. "source": "https://github.com/bcit-ci/CodeIgniter"
  12. },
  13. "require": {
  14. "php": ">=5.2.4",
  15. "smarty/smarty": "^3.1"
  16. },
  17. "suggest": {
  18. "paragonie/random_compat": "Provides better randomness in PHP 5.x"
  19. },
  20. "require-dev": {
  21. "mikey179/vfsStream": "1.1.*",
  22. "phpunit/phpunit": "4.* || 5.*"
  23. },
  24. "autoload": {
  25. "psr-4": {
  26. "App\\": "application/",
  27. "App\\Controllers\\": "application/controllers/"
  28. }
  29. }
  30. }

解释配置项autoload

  1. "autoload": {
  2. "psr-4": {
  3. "app\\": "application/"
  4. }
  5. }

键名为顶级命名空间app\。这里使用app\,第一个\是转义符
键值为命名空间对应的目录application/

更新composer

在CI根目录执行命令
composer update

修改CI配置

system/core/Codelgniter.php 第359行

  1. // Load the base controller class
  2. require_once BASEPATH.'core/Controller.php';
  3. /*新增*/
  4. require_once BASEPATH. 'core/Model.php';

第402行

  1. $e404 = FALSE;
  2. /*新增*/
  3. $classNamespace = 'App\\Controllers\\';
  4. if ($RTR->directory !== NULL) {
  5. $classNamespace .= ucfirst( str_replace('/','\\', $RTR->directory) );
  6. }

第417行

  1. require_once(APPPATH.'controllers/'.$RTR->directory.$class.'.php');
  2. // 添加Controller命名空间
  3. $customizeClass = $classNamespace . $class;
  4. $class = class_exists($customizeClass) ? $customizeClass : $class;

修改 system/core/Model.php 第57行

  1. public function __construct()
  2. {
  3. /*新增*/
  4. log_message('info', 'Model Class Initialized');
  5. }

第77行

  1. /*新增*/
  2. //如果用到db类 就自动连接数据库
  3. if ($key === 'db') {
  4. get_instance()->load->database();
  5. }
  6. return get_instance()->$key;

测试

浏览器访问index.php/demo,如果正常显示This is a msg from class Base.则说明配置成功。

转自:https://www.maoxuner.cn/2017/03/24/ci3-composer.html

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post