17
19

More than 5 years have passed since last update.

【基礎】CodeIgniterでライブラリを作成する

Posted at

この記事はCodeIgniter Advent Calendar 2013 の 2 日目の記事です。

先日に続いて2記事目(さて、これが何回続くか。。

まだまだCodeIgniter Advent Calendar は募集中ですよ!

さて、今回の内容ですが、先日はコアクラスの拡張だったので、本日はライブラリの作成方法でも紹介しましょう。

Creating Libraries

もちろん、公式に書いてあります!!!

ライブラリの作成

ライブラリって?

ある機能を追加したい場合にユーザーが機能を作成・拡張し、それをCodeIgniterでロードできるものです。

ライブラリはCodeIgniter自身が最初から定義しているシステムのライブラリと、ユーザーが作成・拡張を行う事もできます。

今回は、ユーザーがライブラリを作成する方法を紹介します。(ライブラリの話で数日保つんじゃ・・・?)

ユーザーのライブラリ作成方法

さて、ユーザーがライブラリの作成を行う方法ですが、これまた非常に簡単です。

applicationフォルダにある libraries にあるルールでファイルを作るだけです。

これだけ!

ルールも非常に簡単で、

  1. ファイル名は先頭が大文字
  2. クラス名はファイル名と同じ

※作成したライブラリは先頭を小文字でロードすることになります。

では、Basic認証機能を備えたライブラリの作成順序を述べますと

  1. ./application/libraries/ の中にBasic_auth.php を作成する
  2. クラス名をBasic_auth とする
  3. ロードしたいところで $this->load->library('basic_auth'); のコードを挿入
  4. 以後、$this->basic_auth->foo(); のように使用可能

That's it.

文字じゃなくてコードの方がいいですよね。わかります。

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Basic_auth {

    private $_default_user = 'admin';
    private $_default_password = '';

    private $_admin_user;
    private $_admin_password;

    private $_logged_in_key = 'logged_in';

    public function __construct()
    {
        $config =& get_config();
        $this->_admin_user = (isset($config['admin_user']))? $config['admin_user'] : $this->_default_user;
        $this->_admin_password = (isset($config['admin_password'])) ? $config['admin_password'] : $this->_default_password;
    }

    public function login()
    {
        $CI =& get_instance();
        $logged_in = $CI->session->userdata($this->_logged_in_key);
        if ($logged_in !== TRUE)
        {
            if ($this->headers_401())
            {
                $CI->session->set_userdata(array(
                    $this->_logged_in_key => TRUE
                ));
            }
            else
            {
                die("Please enter a valid username and password");
            }
        }
    }

    private function headers_401()
    {
        $CI =& get_instance();
        $user = $this->_admin_user;
        $password = $this->_admin_password;
        if (!$CI->input->server('PHP_AUTH_USER'))
        {
            header('WWW-Authenticate: Basic realm="SMF Studios"');
            header('HTTP/1.0 401 Unauthorized');
        }
        else
        {
            if (($CI->input->server('PHP_AUTH_USER') === $user) && ($CI->input->server('PHP_AUTH_PW') === $password))
            {
                return TRUE;
            }
        }
        return FALSE;
    }

}

/* End of file Basic_auth.php */
/* Location: ./application/libraries/Basic_auth.php */

上記な感じで./application/libraries/Basic_auth.php を作成します。

これを、例えば./application/controllers/admin.php などで使用するときは

class Admin extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->library('basic_auth');
        $this->basic_auth->login();
    }

}

このような感じで使用できます。

めっちゃ簡単ですね。

終わり

作成したライブラリによくわからんget_instance とかあるので、明日はこれを紹介しましょうかね。

次回のCodeIgniter Advent Calendar2013

誰もいないので、すごくざあああああああっとしか書いてないです。
しかし、こんな機会でしかブログっぽいの書かないので良い事でしょう。

17
19
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
19