[Centos7] 安裝php套件管理程式Composer+ Codeignioter3

URL Link //n.sfs.tw/11141

2017-05-26 12:43:48 By 張○○

很多人在用的套件管理程式

安裝Composer[1]

# php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
# php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Installer verified
# php composer-setup.php
All settings correct for using Composer
Downloading...
Composer (version 1.4.2) successfully installed to: /root/composer.phar
Use it: php composer.phar
# php -r "unlink('composer-setup.php');"

 安裝完畢把composer.phar搬到程式路徑中並改名為composer[2]

# mv composer.phar /usr/local/bin/composer

這樣就能在任何位置執行。

或是直接在安裝時加上參數即可

# php composer-setup.php --install-dir=/usr/local/bin --filename=composer

使用composer安裝codeigniter3

到你的專案目錄下,假設目前是空的

# cd /var/www/html
由於這個目錄需要root的權限才能寫入,因此以root的身分執行,若有出現警告可以忽略
# composer create-project codeigniter/framework ./

這樣子會在你的目錄下直接產生codeigniter,如果想指定安裝目錄,把後面的 "./" 改成你指定的目錄即可

# ls

裡面的檔案除了codeigniter本身的以外,下面這些屬於composer的,由[2]的解釋:

composer.json  json 資料格式的設定檔,提供資訊給 Composer,讓它知道要安裝什麼套件,及該套件的版本。

composer.lock 會明確列出這個專案中用到的套件及它的版本,可以用來確保其他人使用到同一個版本號的套件。

vendor/ 存放套件的地方

這樣算是裝好了,刪除說明文件

# rm -rf user_guide
# rm readme.rst

修改套件安裝路徑及把用不到套件刪掉

# vi composer.json

把不要的套件刪掉,vendor/ 是預設的安裝路徑,我把他改到 third_party這個路徑,所以修改完後像這樣。

{
    "config": {
        "vendor-dir": "application/third_party"
    },
        "description": "The CodeIgniter framework",
        "name": "codeigniter/framework",
        "type": "project",
        "homepage": "https://codeigniter.com",
        "license": "MIT",
        "support": {
                "forum": "http://forum.codeigniter.com/",
                "wiki": "https://github.com/bcit-ci/CodeIgniter/wiki",
                "irc": "irc://irc.freenode.net/codeigniter",
                "source": "https://github.com/bcit-ci/CodeIgniter"
        },
        "require": {
                "php": ">=5.2.4"
        },
        "suggest": {
        },
        "require-dev": {
        }
}

刪除已安裝套件

# rm vendor/ -rf

更新套件,目前不裝任何套件

# composer update

# ls /var/www/html/application/third_party

可以看到目錄多出兩個檔案,這樣子就設定好了。

用composer安裝套件並測試

先依照codeigniter的說明做mod_rewrite設定

# cd /var/www/html
# vi .htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /
RewriteRule ^(.*)$ index.php/$1 [L]

編輯設定檔

# vi application/config/config.php

$config['composer_autoload'] = APPPATH . 'third_party/autoload.php';

隨便找個套件試試,隨便試,只是要測試能用能順利使用composer裝的第三方套件

安裝套件

# composer require fireball/jwt

修改controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use \Firebase\JWT\JWT;

class In extends CI_Controller {

    public function a()
    {
       $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('HS256'));
     print_r($decoded);
    }
}

輸出

正確!

其他

移除套件

# composer remove vendor/package

升級到composer 2 [5]

Warning from https://packagist.org: Support for Composer 1 is deprecated and some packages will not be available. You should upgrade to Composer 2. See https://blog.packagist.com/deprecating-composer-1-support/

# composer self-update
Updating to version 2.2.12 (stable channel).
   Downloading (100%)         
Use composer self-update --rollback to return to version 1.4.2

查看composer 版本
# composer --version

安裝多個套件

如果需要裝多個套件,可以使用 require 欄位,多個用 ','來作分隔,例如 composer.json 長得像這樣

{
    "config": {
        "vendor-dir": "public_html/application/third_party"
    },
    "require": {
        "google/apiclient": "^2.0",
        "phpgangsta/googleauthenticator" : "dev-master"
    }
}

再執行

$ composer update

這樣就會在 目前目錄的 public_html/application/third_party 下,建立二個套件。

出現 require 不能安裝的錯誤

Could not find a version of package xxx/yyy matching your minimum-stability (stable). Require it with an explicit version constrai
nt allowing its desired stability.

可採用上面的方式修改設定檔來安裝

參考資料

[1] https://getcomposer.org/download/

[2] Tony Blog http://blog.tonycube.com/2016/12/composer-php.html

[3] https://stackoverflow.com/questions/11883374/how-to-specify-composer-install-path

[4] https://jsnwork.kiiuo.com/archives/2408/php-%E5%9C%A8-codeigniter-%E6%93%8D%E4%BD%9C-composer-%E8%87%AA%E5%8B%95%E8%BC%89%E5%85%A5%E5%A5%97%E4%BB%B6

[5] https://support-acquia.force.com/s/article/4408826090775-Message-Support-for-Composer-1-is-deprecated-and-some-packages-will-not-be-available-while-running-Drupal-upgrade-using-composer