定制 dikki/ci4-di 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dikki/ci4-di

最新稳定版本:0.0.2

Composer 安装命令:

composer require dikki/ci4-di

包简介

Implement and Use Dependency Injection in CodeIgniter 4.

README 文档

README

Robust Dependency Injection for CodeIgniter 4, powered by League Container.

Features

  • PSR-11 Compliant Container.
  • Autowiring: Automatically resolve dependencies for classes without manual configuration.
  • Service Providers: Organize service registrations.
  • Aliases: Bind services to aliases.
  • Shared Services: Support for Singleton pattern.
  • Seamless Integration: Replaces CI4's Controller constructor injection mechanism.

Installation

composer require dikki/ci4-di

Initialize the configuration:

php spark container:publish

This will create app/Config/Container.php where you can configure services.

Setup

Modify app/Config/Services.php to define the codeigniter service override. This ensures the application uses the DI Container for Controller instantiation.

<?php

namespace Config;

use CodeIgniter\Config\BaseService;
use CodeIgniter\Config\App;
use Dikki\Container\Application;

class Services extends BaseService
{
    // ...

    public static function codeigniter(?App $config = null, bool $getShared = true)
    {
        if ($getShared) {
            return static::getSharedInstance('codeigniter', $config);
        }

        $config = $config ?? config('App');

        return new Application($config);
    }
}

Usage

Autowiring

Most of the time, you don't need to do anything. If your Controller injects a service, it will be automatically resolved.

class Home extends BaseController
{
    public function __construct(
        private UserService $userService
    ) {}
}

Manual Configuration

You can explicitly define services in app/Config/Container.php:

// app/Config/Container.php
public array $services = [
    // Interface binding
    'App\Interfaces\UserRepoInterface' => 'App\Repositories\UserRepo',
    
    // Concrete value or class
    'mailer' => 'App\Services\Mailer',
];

public array $shared = [
    // Singleton services
    'db.connection' => 'App\Database\Connection',
];

Service Providers

For complex setup, use Service Providers.

// app/Config/Container.php
public array $providers = [
    'App\Providers\AppServiceProvider',
];

Accessing the Container

You can access the container instance globally:

use Dikki\Container\Container;

$container = Container::getInstance();
$service = $container->get('my_service');

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固