今までWebアプリを作る時には、フロントと管理画面を別々のWebアプリとして作成していたのですが、今回prefixルーティングを使って同一のアプリケーションの中でフロントと管理画面を分けるようにしてみました。
結論から言うと、「管理画面とフロントは別々に作った方が良い」ようです。
CakePHPのAuthコンポーネントは、非常に簡単で便利なのですが、基本的に「ログインしているか否か」を判別しているだけ。
つまり、管理画面にログインするとフロントにもログインしていることになり、逆にフロントでログインすると、管理画面にまでログインしていることになってしまう訳です。
ここでACL(Access Control List)というコンポーネントを使えば、アクセスを制御出来る訳ですが、これがややこしい!
ACL — Cookbook v2.x documentation
そこでACLを攻略するという記事を見つけたのですが、やっぱり管理者と一般ユーザーで分けるのには向かない気がします。
というわけで、管理画面は別に作ることにしました。。。
同じデータベースを使うのに複数のモデルファイルを作るのは面倒なので、bootstrap.phpに以下の記述をすることで共通のモデルファイルを使うことが出来ます。
App::build(array(
'Plugin' => array('/full/path/to/plugins/', '/next/full/path/to/plugins/'),
'Model' => array('/full/path/to/models/', '/next/full/path/to/models/'),
'View' => array('/full/path/to/views/', '/next/full/path/to/views/'),
'Controller' => array('/full/path/to/controllers/', '/next/full/path/to/controllers/'),
'Model/Datasource' => array('/full/path/to/datasources/', '/next/full/path/to/datasources/'),
'Model/Behavior' => array('/full/path/to/behaviors/', '/next/full/path/to/behaviors/'),
'Controller/Component' => array('/full/path/to/components/', '/next/full/path/to/components/'),
'View/Helper' => array('/full/path/to/helpers/', '/next/full/path/to/helpers/'),
'Vendor' => array('/full/path/to/vendors/', '/next/full/path/to/vendors/'),
'Console/Command' => array('/full/path/to/shells/', '/next/full/path/to/shells/'),
'Locale' => array('/full/path/to/locale/', '/next/full/path/to/locale/')
));
モデル以外にも色々なファイルを共通化できるので便利。