สร้างฟอร์ม Login Zend Framework

หมวดสำหรับแบ่งบันความ รู้ต่างๆ จะมีหมวดย่อยๆ ในหมวดนี้ เช่น php, SQL, XML, CSS

Moderator: mindphp, ผู้ดูแลกระดาน

zend_framework
PHP Full Member
PHP Full Member
โพสต์: 41
ลงทะเบียนเมื่อ: 08/03/2011 11:22 pm

สร้างฟอร์ม Login Zend Framework

โพสต์ที่ยังไม่ได้อ่าน โดย zend_framework »

สร้างฟอร์ม Login Zend Framework
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
3. Create database

โค้ด: เลือกทั้งหมด

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');
4. สร้าง Controller ชื่อ application/controllers/AuthController.php

โค้ด: เลือกทั้งหมด

<?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
}
 
5. สร้างไดเรกทอรีและไฟล์ดังนี้ application/views/scripts/auth/login.phtml

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; 
7. Protect pages

โค้ด: เลือกทั้งหมด

function preDispatch()
    {

    $auth = Zend_Auth::getInstance();
    if (!$auth->hasIdentity()) {
        $this->_redirect('auth/login');
        }else {
        $this->view->user = Zend_Auth::getInstance()->getIdentity();  //show user info
    }

 
8. show login 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; ?>
topman
PHP Newbie
PHP Newbie
โพสต์: 5
ลงทะเบียนเมื่อ: 19/11/2012 1:34 pm

Re: สร้างฟอร์ม Login Zend Framework

โพสต์ที่ยังไม่ได้อ่าน โดย topman »

กำลัง สงสัยว่ามันทำยังไงหรอครับ
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 76