Home  >  Article  >  Development Tools  >  codeigniter3 integrates composer management tools, a more elegant approach

codeigniter3 integrates composer management tools, a more elegant approach

藏色散人
藏色散人forward
2019-10-01 13:57:472741browse

Preface, I hope codeigniter4 will be released soon. There is also codeigniter3 for projects. If I want to use so many excellent third-party libraries on github, what should I do? Moreover, these libraries are basically installed through composer. The following column composer tutorial will introduce you to the method of integrating the composer package manager:

codeigniter3 integrates composer management tools, a more elegant approach

1. Install composer, which I won’t go into details here.

2. Create a new composer.json file in the project root directory. For example, I want to use a jwt library in ci now

{
  "require": {
    "firebase/php-jwt": "*"
  }
}

3. This directory Execute from the command line: composer install

or the above 2 steps can be simplified to the following step

composer require firebase/php-jwt

4. Let’s start the formal integration into ci. Here are 2 methods:

Method 1: Simple and crude (not recommended)

require_once './vendor/autoload.php';
//上面这一行添加到index.php的这个位置
require_once BASEPATH.'core/CodeIgniter.php';

Method 2: Elegant (recommended)

application/libraries Create a new MY_Composer. php

<?php
/**
 * 关于MY_Composer的注释
 *
 * @author 新猪
 */
class MY_Composer 
{
    function __construct() 
    {
        include("./vendor/autoload.php");
    }
}

Then modify

$autoload[&#39;libraries&#39;] = array(&#39;MY_Composer&#39;,&#39;database&#39;,&#39;session&#39;);

5 in config/autoload.php and use

<?php
use \Firebase\JWT\JWT;
class TestController extends CI_Controller {
    public function index() {
        $key = "example_key";
        $token = array(
            "iss" => "http://example.org",
            "aud" => "http://example.com",
            "iat" => 1356999524,
            "nbf" => 1357000000
        );
        $jwt = JWT::encode($token, $key);
        $decoded = JWT::decode($jwt, $key, array(&#39;HS256&#39;));
        print_r($decoded);
    }
}

. I hope it will be helpful to everyone.

The above is the detailed content of codeigniter3 integrates composer management tools, a more elegant approach. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete