Zend FrameworkでAdminページを簡単に作るためのコントローラ習作

Zend Frameworkで管理者用の認証ぺージなどを作るための簡単なコントローラです。

[php]
require_once('Zend/Controller/Action.php');
class AdminController extends Zend_Controller_Action
{
public function init()
{
$session = new Zend_Session_Namespace('admin');
if (!$session->auth) {
$this->_forward(‘login’);
}
}

public function indexAction()
{
}

public function loginAction()
{
$session = new Zend_Session_Namespace(‘admin’);
$token = $this->getRequest()->getParam(‘token’);
$pass = $this->getRequest()->getParam(‘pass’);

if ($token === $session->token &&
‘YOUR-PASSWORD’ === $pass) {
$session->auth = true;
$this->_forward(‘index’);
}

$token = md5(mt_rand());
$this->view->token = $token;
$session->token = $token;

}
}
?>

[/php]

init()メソッドはコントローラで1番最初に(コンストラクタで)読み込まれますので、コントローラの初期設定などを入れ込んでおくのに適しています。

LoginAction用のHTMLテンプレートの中に、$tokenを埋め込むフォームを入れておきましょう!

[html]

[/html]

関連する記事:

Powered by

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">