zend framework(zf)框架配置实例

 数据库:zend_project

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(1) NOT NULL auto_increment,
  `uname` varchar(50) character set utf8 NOT NULL,
  `upwd` varchar(50) character set utf8 NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=gb2312 AUTO_INCREMENT=7 ;

zend/library/zend framework类库

zend/index.php

<?php
error_reporting(E_ALL|E_STRICT);
set_include_path('.' .PATH_SEPARATOR .'./library'
.PATH_SEPARATOR .'./application/models/'.PATH_SEPARATOR .get_include_path());
require_once "Zend/Loader/Autoloader.php";
Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
//配置数据库参数,并连接数据库
$config=new Zend_Config_Ini('./application/config/config.ini',null, true);
Zend_Registry::set('config',$config);
$dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());
$dbAdapter->query('SET NAMES gbk');
Zend_Db_Table::setDefaultAdapter($dbAdapter);
Zend_Registry::set('dbAdapter',$dbAdapter);
//设置模板显示路径
$view = new Zend_View();
$view->setScriptPath('./application/views/scripts/');
//注册View
$registry = Zend_Registry::getInstance();
$registry['view'] = $view;
//设置控制器
$frontController =Zend_Controller_Front::getInstance();
$frontController->setBaseUrl('/zend')//设置基本路径
  ->setParam('noViewRenderer', true)
  ->setControllerDirectory('./application/controllers')
  ->throwExceptions(true)
  ->dispatch();
?>

控制器zend/application/controllers/IndexController.php

<?php
class IndexController extends Zend_Controller_Action

    function init()
    {
        $this->registry = Zend_Registry::getInstance();
        $this->view = $this->registry['view'];
        $this->view->baseUrl = $this->_request->getBaseUrl();
 
    }
    function indexAction()
    {            
     $ur=new Users();//实例化数据库类
       //这里给变量赋值,在index.phtml模板里显示
        $this->view->bodyTitle = '<h1>Hello World!</h1>';
        //取到所有数据.二维数组
        $this->view->ur=$ur->fetchAll()->toArray();      
     echo $this->view->render('index.phtml');//显示模版 
    }   
 function addAction(){
    //如果是POST过来的值.就增加.否则就显示增加页面
    if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
       //过滤一些数据.不过这里还有检测一些动作没有做..//请大家加了..我就不多写那么多了.时间关系..
     Zend_Loader::loadClass('Zend_Filter_StripTags');
     $filter=new Zend_Filter_StripTags();
     $uname=$filter->filter(($this->_request->getPost('uname')));
     $upwd=$filter->filter(($this->_request->getPost('upwd')));
     $users=new Users();
     $data=array(
      'uname'=>$uname,
      'upwd'=>$upwd
     ); 
     $users->insert($data);
     unset($data);
     echo '您增加数据成功!请您<a href="http://zuo.ai.xiao.blog.163.com/blog/'.$this->view->baseUrl.'">返回</a>';
     }else{
       echo $this->view->render('add.phtml');//显示增加模版 
     }
    }
   
 public function editAction(){
  $users=new Users();
 $db = $users->getAdapter();
 Zend_Loader::loadClass('Zend_Filter_StripTags');
 $filter=new Zend_Filter_StripTags();
 //同上面addAction
      if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
  $uname=$filter->filter(($this->_request->getPost('uname')));
  $upwd=$filter->filter(($this->_request->getPost('upwd')));
  $id=$filter->filter(($this->_request->getPost('id')));
         $set=array(
   'uname'=>$uname,
   'upwd'=>$upwd
  );
  $where = $db->quoteInto('id = ?', $id);
  //更新表数据
  $users->update($set, $where);
        unset($set);
        echo '您修改数据成功!请您   <a href="http://zuo.ai.xiao.blog.163.com/blog/'.$this->view->baseUrl.'">返回</a>';
    }else{
     $id=$filter->filter(($this->_request->getParam('id')));
     $this->view->users=$users->fetchAll('id='.$id)->toArray();
     echo $this->view->render('edit.phtml');//显示编辑模版 
     }
    }
 
public function delAction()
{    $users=new Users();
    //能过ID删除数据.这里有一些动作没有做.比如说没有ID页面要去哪里.    
    $id = (int)$this->_request->getParam('id');
    if ($id > 0) {
        $where = 'id = ' . $id;
        $users->delete($where);
         echo '您删除数据成功!请您  <a href="http://zuo.ai.xiao.blog.163.com/blog/'.$this->view->baseUrl.'">返回</a>';
    }
  }
}
?>

模型zend/application/models/Users.php

<?php
class Users extends Zend_Db_Table
{
   protected $_name ="users";
   protected $_primary = 'id';
}
?>

显示zend/application/views/scripts/index.phtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>无标题文档</title></head>
<body>
<form id="form1" name="form1" method="post" action="index/add">
<table align="center">
    <tr>
        <td>
          用户名: <input type="text" name="uname" />
        </td>       
        <td>
          密码:<input type="text" name="upwd" />
        </td>       
        <td>
        <input type="submit" name="add_us"  value="提交"/>
        </td>
    <tr/>
</table>
</form>

<table align="center" width="30%">
    <tr bgcolor="#FFFFFF">
        <td align="center">
          用户名
        </td>       
        <td align="center">
          密码
        </td>       
        <td align="center">
          操作
        </td>
    <tr/>
     <?php foreach ($this->ur as $key => $val){ ?>    
         <tr>              
         <td align="center"><?=$val['uname'] ?></td>
         <td align="center"><?=$val['upwd'] ?></td>
         <td align="center">
         <?php echo "<a href='http://zuo.ai.xiao.blog.163.com/blog/index/edit/id/{$val['id']}'>修改</a>|<a href='http://zuo.ai.xiao.blog.163.com/blog/index/del/id/{$val['id']}'>删除</a>";?>        
         </td>       
        <tr/>
    <?php } ?>
</table> 
</body>
</html>
zend/application/views/scripts/edit.phtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>无标题文档</title></head>
<body>
 <?php foreach ($this->users as $key => $val){ ?>    
<form id="form1" name="form1" method="post" action="index/edit">
<table align="center">
    <tr>
        <td>
        <input type="hidden" name="id" value="<?=$val['id']?>" />
          用户名: <input type="text" name="uname" value='<?=$val['uname'] ?>'/>
        </td>
    </tr>
     <tr>
        <td>
          密码:<input type="text" name="upwd" value='<?=$val['upwd'] ?>'/>
        </td>
        <td>
         <input type="submit" name="add_us"  value="修改"/>
        </td>
    </tr>
</table>
</form>
  <?}?> 
</body>
</html>

zend/application/config/config.ini

[general]
db.adapter=PDO_MYSQL
db.config.host=localhost 
db.config.username=root 
db.config.password=root
db.config.dbname=zend_project

zend/.htaccess

RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
php_value include_path "/path/to/library"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值