Home > Development Tools > composer > codeigniter3 integrates composer management tools

codeigniter3 integrates composer management tools

藏色散人
Release: 2020-02-21 13:49:35
forward
2584 people have browsed it

codeigniter3 integrates composer management tools

Preface, I hope codeigniter4 will be released soon. There is also codeigniter3 for projects. 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 introduces the method of integrating the composer package manager:

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

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": "*"
  }
}
Copy after login

3. Execute the command line in this directory: composer install

Or the above 2 steps can be simplified into the following step

composer require firebase/php-jwt
Copy after login

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

Method 1: Simple and crude (not Recommended)

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

Method 2: Elegant (recommended)

application/librariesCreate a new MY_Composer.php

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

and then modify it in config/autoload.php

$autoload['libraries'] = array('MY_Composer','database','session');

5. Finished using

<?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);
    }
}
Copy after login

, I hope it will be helpful to everyone.

For more programming related content, please pay attention to the Programming Introduction column on the php Chinese website!

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

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Composer failed to install TP51
From 1970-01-01 08:00:00
0
0
0
PHP study installation composer cannot be used
From 1970-01-01 08:00:00
0
0
0
php - Error using composer
From 1970-01-01 08:00:00
0
0
0
ThinkPHP Why use composer?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template