我使用的作業環境ubuntu 16.04 + php5.6
一、 安裝composer
- 更新來源包的資料
sudo apt-get update
- 安裝預備套件
sudo apt-get install curl php-cli php-mbstring git unzip
- 下載與安裝Composer
curl -sS https://getcomposer.org/installer -o composer-setup.php
- 配置Composer全域環境設定
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
p.s : 安裝的composer的指令composer,預設安裝在 /usr/local/bin 。
- 產生composer.phar
php composer-setup.php
二、 安裝CI與Composer之間需要的擴充套件
安裝CI的時候如果出現此問題表示有很多相依套件沒裝
Problem 1 - Installation request for magento/product-enterprise-edition 2.0.2 -> satisfiable by magento/product-enterprise-edition[2.0.2]. - magento/product-enterprise-edition 2.0.2 requires ext-gd * -> the requested PHP extension gd is missing from your system. To enable extensions, verify that they are enabled in those .ini files: - /etc/php5/cli/php.ini - /etc/php5/cli/conf.d/05-opcache.ini - /etc/php5/cli/conf.d/10-pdo.ini - /etc/php5/cli/conf.d/20-curl.ini - /etc/php5/cli/conf.d/20-imap.ini - /etc/php5/cli/conf.d/20-json.ini - /etc/php5/cli/conf.d/20-mcrypt.ini - /etc/php5/cli/conf.d/20-pdo_pgsql.ini - /etc/php5/cli/conf.d/20-pgsql.ini - /etc/php5/cli/conf.d/20-pspell.ini - /etc/php5/cli/conf.d/20-readline.ini You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
sudo apt-get update sudo apt-get install mcrypt php5.6-mcrypt sudo apt-get upgrade sudo apt-get install php-mbstring sudo apt-get install phpunit sudo apt-get install php5-gd sudo apt-get install php5-intl sudo apt-get install php5-xsl sudo apt-get install php5.6-xml sudo service apache2 restart
三、在Codeigniter操作Composer自動載入套件
- 建立CI專案
composer create-project codeigniter/framework test
- 下載套件
- composer.json 是設定檔,打開如下
{"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": {
"paragonie/random_compat": "Provides better randomness in PHP 5.x"
},
"require-dev": {
"mikey179/vfsStream": "1.1.*"}
}
|
在 require 添加我們想要下載的套件,例如 monolog/monolog
5
6
|
"require": {
"php": ">=5.2.4",
"monolog/monolog": "1.0.*"
},
|
接著 update 套件
2
3
4
|
cd test
composer update
|
可以看到路徑 vendor/monolog 已經下載下來,而且還會看到依賴套件。
打開自動載入
修改 application/config/config.php
1
2
3
|
$config['composer_autoload'] = 'vendor/autoload.php';
|
注意!若將 FALSE 改成 TRUE,代表你的路徑在 application/vendor/autoload.php。但我們的 vendor 在專案目錄,所以要自行填入路徑。當然官方預設是將 vendor 放在 application 內也是不錯的方法,要搬移或升級的時候也是比較明確。
測試是否如期的自動加載,打開 application/controllers/Welcome.php
2
3
4
5
6
7
8
9
10
11
|
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
}
|
寫寫看安裝的 Monolog (這是一個功能多的 log 工具) 能否觸發。
1
2
3
4
5
6
7
8
|
public function index()
{
$log = new Monolog\Logger('name');
$log->pushHandler(new Monolog\Handler\StreamHandler('app.log', Monolog\Logger::WARNING));
$log->addWarning('Hello World', []);
}
|
瀏覽網址,執行後就會看到專案底下有個 app.log 裡面內容如
1
2
3
|
[2016-10-18 14:49:10] name.WARNING: Hello World [] []
|
參考資料 :
1. Composer安裝
https://www.digitalocean.com/community/tutorials/how-to-install-and-use-composer-on-ubuntu-16-04
2. 相依套件安裝
https://stackoverflow.com/questions/30868608/laravel-production-issue-updating-composer-with-laravel-4-1-x
https://laracasts.com/discuss/channels/forge/error-while-updating-to-php-70-on-forge
https://community.magento.com/t5/Installing-Magento-2-x/To-enable-extensions-verify-that-they-are-enabled-in-those-ini/td-p/31252