mjamilasfihani/conquer-container 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

mjamilasfihani/conquer-container

最新稳定版本:v2.2.5

Composer 安装命令:

composer require mjamilasfihani/conquer-container

包简介

Containerized Dependency Injection for CodeIgniter 4 Controller.

README 文档

README

Containerized Dependency Injection for CodeIgniter 4 Controller.

Warning! This library crack the core point of CodeIgniter 4, consider to use it at your own risk!

Coverage Status

Prerequisites

Usage of Conquer\Container requires the following:

Installation

Use the package manager composer to install.

composer require mjamilasfihani/conquer-container

Usage

Let's say you have app/Libraries/ExampleLibrary.php file, and you want to load in __construct() function

in your controller without initializing it manually. Than this library is yours.

Can I imagine your controller? Thank you :

namespace App\Controllers;

use App\Controllers\BaseController;
use App\Libraries\ExampleLibrary;

class Home extends BaseController
{
    /**
     * @var \App\Libraries\ExampleLibrary
     */
    protected ExampleLibrary $exampleLibrary;

    // This is your old constructor isn't?
    //
    // /**
    //  * Constructor
    //  */
    // public function __construct()
    // {
    //     $this->exampleLibrary = new ExampleLibrary();
    // }

    /**
     * This will be your new Constructor
     *
     * @param \App\Libraries\ExampleLibrary $exampleLibrary
     */
    public function __construct(ExampleLibrary $exampleLibrary)
    {
        $this->exampleLibrary = $exampleLibrary;
    };
    
    /**
     * Display Homepage
     *
     * @return string
     */
    public function index(): string
    {
        // even it has equal result, depend how like you call your library :)
        $this->exampleLibrary;

        return view('welcome_message');
    }
}

If you have AnotherExampleLibrary.php and it need ExampleLibrary class in the constructor,

feel free to add it. Because it has been supported in since v2.0.0

Here is what I mean :

namespace App\Libraries;

use App\Libraries\ExampleLibrary;

class AnotherExampleLibrary
{
    protected ExampleLibrary $exampleLibrary;

    /**
     * Constructor
     *
     * @param \App\Libraries\ExampleLibrary $exampleLibrary
     */
    public function __construct(ExampleLibrary $exampleLibrary)
    {
        $this->exampleLibrary = $exampleLibrary;
    }

    public function anotherExampleMethod()
    {
        // you have a power from your parent class
        $exampleLibrary = $this->exampleLibrary;

        ...
    }
}

Now you can call it with no worries from your controller :

public function __construct(AnotherExampleLibrary $anotherExampleLibrary)
{
    // this use case is very help full for implement the repository pattern
    $this->anotherExampleLibrary = $anotherExampleLibrary;
}

Notes

Remember one thing! Doing container like this is not officially supported by CodeIgniter 4,

since it has different structure do not judge me if you got an error for calling

the CodeIgniter 4 library use this method. (Do It By Your Own Risk)

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-01-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固