time 2017/10/17
CakePHPを勉強している時に、「appディテクトリを複数管理できたらいいな~」と思いました。
理由としては、複数の管理画面を持つシステム開発や同じサーバー内で複数のサイトを運営する場合などを想定したからです。
appを複数管理なので、CakePHPのコアを共有することで生まれるメリット(サーバーリソースの節約やCakePHPのバージョンアップ対応)もあります。
というわけで、さっそくやってみましょ。
まずは、「公開ディレクトリ上にCakePHPを配置した状態で、サイトを訪問するユーザー用と運営者用のappディレクトリを作成」してみます。
『http://cakephp-test.yasigani-ni.com/』にアクセスすると、サイトを訪問するユーザー用アプリケーションを表示。
『http://cakephp-test.yasigani-ni.com/admin/』にアクセスすると、運営者用アプリケーションを表示とします。
『app』ディレクトリをコピーして『admin』ディレクトリを作成。
/ドキュメントルート / app // 訪問者用アプリケーション / admin // 運営者用アプリケーション / cake / plugins / vendors .htaccess index.php
この状態で『http://cakephp-test.yasigani-ni.com/』アクセスしてみると・・・
問題なく『訪問者用アプリケーション』を表示します。
この状態で『http://cakephp-test.yasigani-ni.com/admin/』アクセスしてみると・・・
『運営者用アプリケーション』を表示します。
『app』ディレクトリに『webroot』を配置した状態だと、このままで複数管理することができます。
CakePHPって、なんて便利なフレームワーク!!コピーだけで済んじゃいます。
次は、「webrootだけを公開ディレクトリ、残りを非公開ディレクトリに配置した状態で複数管理」してみます。
公開ディレクトリを『/ html』、非公開ディレクトリを『/ cgi-bin』。
訪問者用アプリケーションディレクトリを『/ user_app』、運営者用アプリケーションディレクトリを『/ admin_app』。
『http://cakephp-test.yasigani-ni.com/』にアクセスすると、サイトを訪問するユーザー用を表示。
『http://cakephp-test.yasigani-ni.com/admin/』にアクセスすると、運営者用を表示とします。
現在のディレクトリ構造は以下の通りです。
▼▼非公開ディレクトリ移動前
/ cgi-bin // 非公開ディレクトリ / html // 公開ディレクトリ / app // アプリケーション / cake // CakePHPコア / plugins / vendors .htaccess index.php
まずは、運営者用のwebrootを配置する為に『/ admin』を『/ html』に作成します。
作成できたら、『/ app / webroot』内に配置されているディレクトリやファイルを、『/ html』と『/ admin』内に移動させます。
これで、訪問者用と運営者用の『webroot』の役割を果たすディレクトリの準備ができました。
次は、「残りを非公開ディレクトリに配置」します。
CakePHPのコアディレクトリやアプリケーションディレクトリを『/ cgi-bin』に移動させます。
『/ app』ディレクトリをコピーして、『/ user_app』と『/ admin_app』を作成します。
▼非公開ディレクトリ移動後
/ cgi-bin // 非公開ディレクトリ / user_app // 訪問者用アプリケーション / admin_app // 運営者用アプリケーション / cake // CakePHPコア / plugins / vendors / html // 公開ディレクトリ / admin // 運営者用webroot / css / files / img / js .htaccess index.php css.php test.php
上記のようなディレクトリ構造になったと思います。
アプリケーションディレクトリやCakePHPコアを移動させたので、『/ html / index.php』と『/ html / admin / index.php』を編集します。
(詳しい説明はCakePHP webroot以外を非公開ディレクトリに移動するに書いています。)
▼『/ html / index.php』の編集箇所
/** * The full path to the directory which holds "app", WITHOUT a trailing DS. * */ if (!defined('ROOT')) { define('ROOT', 'C:' . DS . 'cgi-bin'); } /** * The actual directory name for the "app". * */ if (!defined('APP_DIR')) { define('APP_DIR', 'user_app'); } /** * The absolute path to the "cake" directory, WITHOUT a trailing DS. * */ if (!defined('CAKE_CORE_INCLUDE_PATH')) { define('CAKE_CORE_INCLUDE_PATH', ROOT); }
▼『/ html / admin / index.php』の編集箇所
/** * The full path to the directory which holds "app", WITHOUT a trailing DS. * */ if (!defined('ROOT')) { define('ROOT', 'C:' . DS . 'cgi-bin'); } /** * The actual directory name for the "app". * */ if (!defined('APP_DIR')) { define('APP_DIR', 'admin_app'); } /** * The absolute path to the "cake" directory, WITHOUT a trailing DS. * */ if (!defined('CAKE_CORE_INCLUDE_PATH')) { define('CAKE_CORE_INCLUDE_PATH', ROOT); }
『ROOT』は、非公開ディレクトリへのパスに変更。
『APP_DIR』は、それぞれのアプリケーションディレクトリ名に変更。
『CAKE_CORE_INCLUDE_PATH』は、『ROOT』と同じなのでそのままにしてあります。
「webrootだけを公開ディレクトリ、残りを非公開ディレクトリに配置した状態で複数管理」するのも、たったこれだけの作業でできます。
あとは、バリバリ開発するだけですね^^