Written by Manabu Bannai

【CodeIgniter】Composer(コンポーザー)を利用/インストールする方法

CodeIgniter PHP PROGRAMMING

CodeIgniterでComposer(コンポーザー)をインストール/利用する方法です。
備忘録的なまとめです。

Composerをインストールする

CodeIgniterのアプリケーションディレクトリに移動します

$ cd /Applications/MAMP/htdocs/practice

その後に、Composer(コンポーザー)のインストールコマンドを実行します。

$ curl -sS https://getcomposer.org/installer | php

成功すると以下のメッセージが表示されます。

#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /Applications/MAMP/htdocs/practice/composer.phar
Use it: php composer.phar

php composer.pharでComposerを実行できます。

$ php composer.phar
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ / __ `__ / __ / __ / ___/ _ / ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
____/____/_/ /_/ /_/ .___/____/____/___/_/
                    /_/
Composer version a8adbfeb9fc7861deade782938222714168a22a8 2014-09-05 16:28:50

Usage:
  [options] command [arguments]

Options:
  --help           -h Display this help message.
  --quiet          -q Do not output any message.
  --verbose        -v|vv|vvv Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
  --version        -V Display this application version.
  --ansi              Force ANSI output.
  --no-ansi           Disable ANSI output.
  --no-interaction -n Do not ask any interactive question.
  --profile           Display timing and memory usage information
  --working-dir    -d If specified, use the given directory as working directory.

Available commands:
  about            Short information about Composer
  archive          Create an archive of this composer package
  browse           Opens the package's repository URL or homepage in your browser.
  clear-cache      Clears composer's internal package cache.
  clearcache       Clears composer's internal package cache.
  config           Set config options
  create-project   Create new project from a package into given directory.
  depends          Shows which packages depend on the given package
  diagnose         Diagnoses the system to identify common errors.
  dump-autoload    Dumps the autoloader
  dumpautoload     Dumps the autoloader
  global           Allows running commands in the global composer dir ($COMPOSER_HOME).
  help             Displays help for a command
  home             Opens the package's repository URL or homepage in your browser.
  init             Creates a basic composer.json file in current directory.
  install          Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  licenses         Show information about licenses of dependencies
  list             Lists commands
  remove           Removes a package from the require or require-dev
  require          Adds required packages to your composer.json and installs them
  run-script       Run the scripts defined in composer.json.
  search           Search for packages
  self-update      Updates composer.phar to the latest version.
  selfupdate       Updates composer.phar to the latest version.
  show             Show information about packages
  status           Show a list of locally modified packages
  update           Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  validate         Validates a composer.json

しかし、このままだと該当フォルダ内でしかComposerが利用できません。なので、Composerをグローバルに移行します。

$ sudo mv composer.phar /usr/local/bin/composer

これでグローバルに移行完了しました。
$ composerでComposer(コンポーザー)を実行できます。

※よくあるミス①:/usr/local/binが存在しない場合
$ mkdir /usr/local/binで作成しましょう。

※よくあるミス②:-bash: composer: command not foundと表示される場合
.bash_profileにalias composer="php /usr/local/bin/composer.phar"と記載しましょう。.bash_profileがない場合は、ターミナルで$ touch .bash_profileと打ち込めば作成できます。

CodeIgniterでComposer(コンポーザー)をつかってみる

TextileのComposerを使ってみます。
netcarver/textile – Packagist

dev-master / 3.6.x-devを使います。
以下をコピー。

require: "netcarver/textile": "3.6.*@dev"

applicationと同階層にcomposer.jsonファイルを作成します

以下を記述します。

{
	"require":{
		"netcarver/textile": "3.6.*@dev"
	}
}

applicationの階層でインストールコマンドを実行します

composer install

ちょっと時間がかかりますが、次のメッセージがでれば完了です。

Loading composer repositories with package information
Installing dependencies (including require-dev)

  - Installing netcarver/textile (dev-master a6d19cb)
    Cloning a6d19cbaf2bd47eb2df460f6f9438ddeacd7e7f6

Writing lock file
Generating autoload files

CodeIgniterのindex.phpファイルでオートロードする

applicationの階層にあるindex.phpファイルに以下を追記します。

<?php

require 'vendor/autoload.php';

以上で準備は完了です。
welcomeコントローラーを使ってテストしてみましょう。
編集ファイル:application/controllers/welcome.php

textileThis($string);

		// $this->ion_auth->login("[email protected]", 'password');

		// if( $this->ion_auth->logged_in() == false ){
		// 	redirect ('user/login');
		// }
	}

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

結果は以下です。
きれいなテキスタイルができました( ◜◡ ̄)b

composer

※P.S:無料メルマガで発信中:過去の僕は「ブログ発信で5億円」を稼ぎました。次は「30億円」を目指します。挑戦しつつ、裏側の思考を「メルマガ」から発信します。不満足なら1秒で解約できます。無料登録は「こちら」です。