Written by Manabu Bannai

これからCodeIgniterをはじめる方へ 〜初期セットアップの手順〜

CodeIgniter PROGRAMMING

CodeIgniterの初期セットアップ手順をまとめていきます。
これからCodeIgniterをはじめる方の参考になればと思います。

まずは、CodeIgniterをダウンロード

» CodeIgniter / EllisLab

CodeIgniterのフォルダ名を変更する

変更前:CodeIgniter_2.2.0
変更後:basicsite

もちろん名前はなんでもOKです。
当記事では『basicsite』というフォルダ名にしました。

.htaccessを作成

applicationフォルダと同じ階層におきます。


    RewriteEngine On
    RewriteBase /basicsite/  //フォルダ名に合わせて変更する

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]



    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php

フロントコントローラのURLを設定

編集ファイル:application/config/config.php(29行目)

//$config['index_page'] = 'index.php';
$config['index_page'] = '';

フロントコントローラのURLに関して。
通常は「index.php」を設定しますが、 「mod_rewrite」を利用している場合は空白にします。この設定は「$this->config->site_url()」メソッドと「URLヘルパ」の「site_url()」関数で利用される以外、他の動作に影響はありません。従って、これらのメソッドと関数を利用しなければ設定する必要はありません。

※詳しく知りたい人は別途ググってみてください。

デフォルトのコントローラーを変更する

編集ファイル:application/config/routes.php

//$route['default_controller'] = "welcome";
$route['default_controller'] = "site";

siteコントローラーファイルを動かしてみる

新規ファイル(site.php)を作成します。
場所:application/controllerにsite.php

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

class Site extends CI_Controller {
	public function index(){
		echo "hello world";		//お決まりのhello world
	}
}

以上でCodeIgniterの初期セットアップは完了です( ՞ٹ՞)

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