CakePHP の AuthComponent を admin Routing と組み合わせる
AuthComponent の詳しい使い方はマニュアルに書いてあるので省略
admin routing を有効にして、admin_*** っていう action の時だけ認証かけたい。
ググってもそれらしきものがなかったので忘れないうちに書いとく。
AppController を下記のようにすれば OK。
class AppController extends Controller {
var $components = array('Auth');
function beforeFilter() {
if (Configure::read('Routing.admin') && isset($this->params['admin'])) {
// AuthComponent の設定とかあれば書く
$this->Auth->fields = array('username'=>'name', 'password'=>'passwd');
} else {
$this->Auth->allow($this->params['action']);
}
}
}
単純に admin が含まれてなかったら動的に allow してるだけ。
これで一々 action 名を allow しなくていい。
はじめて使ったけど、AuthComponent めちゃくちゃ便利だ。
コメントする