1. สร้างไฟล์ bootstrap
โค้ด: เลือกทั้งหมด
<?php
define('ROOT_DIR', dirname(dirname(__FILE__)));
//Error Reporting
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 'on');
//Declare PATH Type1
//Modify include PATH
ini_set('include_path', ini_get('include_path')
. PATH_SEPARATOR . '../application/models'
. PATH_SEPARATOR . '../library');
// Zend Framework Include for ZF 1.7.3 or below
//require_once "Zend/Loader.php";
//Zend_Loader::loadClass('Zend_Controller_Front');
//Zend_Loader::registerAutoload();
// Zend Framework Include for ZF 1.8.0 or above
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->setFallbackAutoloader(true);
$loader->suppressNotFoundWarnings(false);
// load configuration
$config = new Zend_Config_Ini('../application/config.ini', 'general');
$registry = Zend_Registry::getInstance();
$registry->set('config', $config);
// setup database
$db = Zend_Db::factory($config->db);
Zend_Db_Table::setDefaultAdapter($db);
Zend_Registry::set('db', $db);
//Layout & Dojo
//$layout = Zend_Layout::startMvc(array('layoutPath' => ROOT_DIR.'/application/views/layouts'));
// Tell the view where it finds Zend_Dojo ViewHelper
//$layout->getView()->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
//get the front contorller instance
Zend_Loader::loadClass('Zend_Controller_Front');
$front = Zend_Controller_front::getInstance();
$front->throwExceptions(true);
$front->setControllerDirectory('../application/controllers');
//Go Go Go
$front->dispatch();
?>
2. application/config.ini
โค้ด: เลือกทั้งหมด
[general]
db.adapter = PDO_MYSQL
db.params.host = localhost
db.params.username = zend15
db.params.password = 15
db.params.dbname = zend15
โค้ด: เลือกทั้งหมด
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`real_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=tis620 AUTO_INCREMENT=2 ;
--
-- dump ตาราง `users`
--
INSERT INTO `users` (`id`, `username`, `password`, `real_name`) VALUES
(1, 'zend15', '15', 'Mr.Zend15 Framework');
โค้ด: เลือกทั้งหมด
<?php
class AuthController extends Zend_Controller_Action
{
function init()
{
$this->initView();
$this->view->baseUrl = $this->_request->getBaseUrl();
$this->view->user = Zend_Auth::getInstance()->getIdentity();
}
function indexAction()
{
$this->_redirect('/auth/login');
}
function loginAction()
{
$this->view->message = '';
if ($this->_request->isPost()) {
// collect the data from the user
Zend_Loader::loadClass('Zend_Filter_StripTags');
$filter = new Zend_Filter_StripTags();
$username = $filter->filter($this->_request->getPost('username'));
$password = $filter->filter($this->_request->getPost('password'));
if (empty($username)) {
$this->view->message = 'Please provide a username.';
} else {
// setup Zend_Auth adapter for a database table
Zend_Loader::loadClass('Zend_Auth_Adapter_DbTable');
$db = Zend_Registry::get('db');
$authAdapter = new Zend_Auth_Adapter_DbTable($db);
$authAdapter->setTableName('users');
$authAdapter->setIdentityColumn('username');
$authAdapter->setCredentialColumn('password');
// Set the input credential values to authenticate against
$authAdapter->setIdentity($username);
//$authAdapter->setCredential(md5($password)); //for support md5 password
$authAdapter->setCredential($password);
// do the authentication
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
if ($result->isValid()) {
// success : store database row to auth's storage system
// (not the password though!)
$data = $authAdapter->getResultRowObject(null, 'password');
$auth->getStorage()->write($data);
$this->_redirect('/');
} else {
// failure: clear database row from session
$this->view->message = 'Login failed.';
}
}
}
$this->view->title = "Log in";
$this->render();
}
function logoutAction()
{
Zend_Auth::getInstance()->clearIdentity();
$this->_redirect('/auth/login');
}//logout
}
6. แสดงข้อมูลlogin # // get the user info from the storage (session)
โค้ด: เลือกทั้งหมด
$userInfo = Zend_Auth::getInstance()->getStorage()->read();
echo "@debug:". $userInfo->user_name."-".$userInfo->tcode."-".$userInfo->flage;
โค้ด: เลือกทั้งหมด
function preDispatch()
{
$auth = Zend_Auth::getInstance();
if (!$auth->hasIdentity()) {
$this->_redirect('auth/login');
}else {
$this->view->user = Zend_Auth::getInstance()->getIdentity(); //show user info
}
โค้ด: เลือกทั้งหมด
<?php if($this->user) : ?>
<p id="logged-in">Welcome: <?php
echo $this->escape($this->user->real_name);?><br />
<a href="<?php echo $this->baseUrl ?>/auth/logout">Logout</a></p>
<?php endif; ?>