CodeIgniterのデフォルト挙動では、WebサーバーのドキュメントルートにCodeIgniterのシステムパスやアプリケーションパスが設定されています。
こういったライブラリをドキュメントルート内に置くのは望ましくないため、できればドキュメントルート外に設置したいところ。
これを行うためにはindex.phpを編集し、$system_pathと$application_folderのパスを通す必要があります。
今回は、CodeIgniterをドキュメントルート外に設置するための設定を紹介します。
CodeIgniterをドキュメントルート外に設置
まずはCodeIgniterをドキュメントルート外に設置します。今回のディレクトリ構成は以下のようになります。
_lib |- CodeIgniter/ |- application/ |- system/ DocRoot/(ドキュメントルート) |- index.php
index.phpはドキュメントルートに設置します。
また、systemディレクトリとapplicationディレクトリをドキュメントルートのひとつ上の階層に_lib/CodeIgniterディレクトリを作って格納します。
$system_pathの編集
まずはindex.phpの100行目(CodeIgniter3.1.6)に記載のある$system_pathを編集します。
/* *--------------------------------------------------------------- * SYSTEM DIRECTORY NAME *--------------------------------------------------------------- * * This variable must contain the name of your "system" directory. * Set the path if it is not in the same directory as this file. */ $system_path = 'system'; ↓ $system_path = '../_lib/CodeIgniter/system';
$system_pathはCodeIgniterのコアとなるシステムへのパスです。
$application_folderの編集
次に、index.phpの117行目(CodeIgniter3.1.6)の$application_folderを編集します。
/* *--------------------------------------------------------------- * APPLICATION DIRECTORY NAME *--------------------------------------------------------------- * * If you want this front controller to use a different "application" * directory than the default one you can set its name here. The directory * can also be renamed or relocated anywhere on your server. If you do, * use an absolute (full) server path. * For more info please see the user guide: * * https://codeigniter.com/user_guide/general/managing_apps.html * * NO TRAILING SLASH! */ $application_folder = 'application'; ↓ $application_folder = '../_lib/CodeIgniter/application';
$application_folderはCodeIgniterにおいて各アプリケーションのコントローラや設定を格納します。
ひとつのCodeIgniterで複数のサイトを構築する場合には、applicationディレクトリを増やし、各サイトの名称などにすると楽です。
// サイト1 $application_folder = '../_lib/CodeIgniter/app1'; // サイト2 $application_folder = '../_lib/CodeIgniter/app2';
表示確認
パスが適切に設定されていれば以下のようにCodeIgniterが表示されます。
パスの設定がおかしい場合は以下のように様々なエラーが出ますので、エラー内容に応じて適宜パスの修正を行いましょう。
$system_pathが適切でない場合
Your system folder path does not appear to be set correctly. Please open the following file and correct this: index.php
$application_folderが適切でない場合
Your application folder path does not appear to be set correctly. Please open the following file and correct this: index.php
コメント