CakePHP1.2で管理者用の静的ページを作る(認証付き)

言わんとしていることがわかるかなー。

CakePHP1.2だと静的ページを表示したい時ってPagesController使って表示出来るんだけど、管理者用のページも作りたい訳。

つうことで以下のページを参考に実装してみた。
"プレフィックスルーティング時のpages_controllerの挙動" フォーラム - CakePHP Users in Japan
actionshrimp.com ≫ CakePHP: PagesController with Admin Routing

まずはPagesControllerを継承したコントローラを作る。
# app/controllers/pages_ex_controller.php

<?php
App::import('Controller', 'Pages');

class PagesExController extends PagesController {

	var $components = array('Auth');

	function beforeFilter(){
		if(Configure::read('Routing.admin') && empty($this->params[Configure::read('Routing.admin')])){
			$this->Auth->allow();
		}
	}

	function admin_display() {
		$path = func_get_args();
		if ($path[0] != 'admin') {
			//This adds admin to the beginning of the path so the pages controller will look in the 'admin' folder in pages directory
			$path = array_merge((array)'admin', $path);
		} else {
			//This removes admin from the beginning if it's there already, and sends the request round again so we end up with URLs that look like app/admin/pages/x
			//when app/admin/pages/admin/x is requested somehow.
			$path = array_slice($path, 1);
			$this->redirect(array_merge(array('controller' => 'pagesEx', 'action' => 'display', 'admin' => true), $path));
		}
		call_user_func_array(array($this, 'parent::display'), $path);
	}
}
?>

一応
/admin/pages/*

ってパスでアクセスして来ても認証されるようにAuthコンポーネント読み込んでる。
まあ、この辺は通常のControllerクラスと一緒なので、お好みで。

ルーティングを設定してやる
# app/config/routes.php

/**
 * admin.
 */
	Router::connect('/admin', array('controller' => 'pagesEx', 'action' => 'display', 'admin' => true, 'home'));
	Router::connect('/admin/pages/*', array('controller' => 'pages', 'action' => 'display', 'admin' => true));

この設定だと
/admin

で管理者用のページ(app/views/pages/admin/home.ctp)へアクセスできるようになる

あんまりネット上に情報が無かったけど、みんないらんのかな。
僕なんかは、管理者のトップページなんかは静的ページ欲しかったりするんだけど。

コメント

アーカイブ

2010

2009

2008

2007

コンタクト

longkey1[at]gmail[dot]com