この記事ではパッケージ管理ツールのComposerを使用してCodeigniter3のインストールした上で認証ライブラリであるIon-Authのセットアップ方法を説明しています。
インストール
Codeigniterのインストール
次のComposerコマンドでCodeigniter3をインストールします、
# composer create-project kenjis/codeigniter-composer-installer <インストール先ディレクトリ名>
Ion-Authのインストール
次のComposerコマンドでIon-Authをインストールします。
# cd <インストール先ディレクトリ名>
# php bin/install.php ion-auth 2
データベースの設定
データベースのスキーマを管理する機能を追加します。
コマンド実行ファイルの作成
以下のファイルを/application/controllers/内に設置します。
Migrate.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Migrate extends CI_Controller {
function __construct()
{
parent::__construct();
if(! $this->input->is_cli_request()) {
show_404();
exit;
}
$this->load->library('migration');
}
function current()
{
if ($this->migration->current()) {
log_message('error', 'Migration Success.');
} else {
log_message('error', $this->migration->error_string());
}
}
function rollback($version)
{
if ($this->migration->version($version)) {
log_message('error', 'Migration Success.');
} else {
log_message('error', $this->migration->error_string());
}
}
function latest()
{
if ($this->migration->latest()) {
log_message('error', 'Migration Success.');
} else {
log_message('error', $this->migration->error_string());
}
}
}
マイグレーションの設定
application/config/migration.phpを次のように編集します。
<?
// マイグレーションを有効化
$config['migration_enabled'] = TRUE;
// シーケンス番号で管理
$config['migration_type'] = 'sequential';
データベースの接続設定
application/config/database.phpにデータベースの接続情報を設定します。
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '<ユーザ名>',
'password' => '<パスワード>',
'database' => '<データベース名>',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Migrationコマンドの発行
次のコマンドをターミナル上で実行してDBスキーマをデータベースへ反映します。
# cd public
# php index.php Migrate latest
ブラウザからアクセス確認
ブラウザから次へアクセスして確認します。
http://www.example.com/auth/login

ログイン確認
次のアカウントでログインできる事を確認します。
ユーザー名:admin@admin.com
パスワード:password
以上で終了です。
コメント