框架zend framework 的研究实例

index.php 页面

<?php
 error_reporting(E_ALL|E_STRICT); //在开启错误报告

 date_default_timezone_set('Asia/Shanghai'); //配置地区

 set_include_path('.' .PATH_SEPARATOR .'./library'.PATH_SEPARATOR .'./application/models/'.PATH_SEPARATOR . get_include_path());  //配置环境路径

 require_once "Zend/Loader/Autoloader.php";  //载入zend框架
 Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true); //静态载入自动类文件

 $registry = Zend_Registry::getInstance(); //静态获得实例
 $view = new Zend_View(); //实例化zend 模板
 $view->setScriptPath('./application/views/scripts/');//设置模板显示路径
 $registry['view'] = $view;//注册View
 
    //  设置数据库连接
 $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 utf8');
 Zend_Db_Table::setDefaultAdapter($dbAdapter);
 Zend_Registry::set('dbAdapter',$dbAdapter);
 
 //  设置控制器
 $frontController =Zend_Controller_Front::getInstance();

 $frontController->setBaseUrl('/zendframework')//设置基本路径,这里面是你的项目的名字
     ->setParam('noViewRenderer', true)
     ->setControllerDirectory('./application/controllers')
     ->throwExceptions(true)
     ->dispatch();
?>

IndexController.php页面

<?php
class IndexController extends Zend_Controller_Action
{

 function init() //__construct 代替初始化函数
    {
        $this->registry = Zend_Registry::getInstance();
        $this->view = $this->registry['view'];
        $this->view->baseUrl = $this->_request->getBaseUrl();

    }

 
 function indexAction()
    {
       //实例化
       $content = new Content();
       $db = $content->getAdapter();
       //  查找所有的信息,并赋值给变量info
       $where = $db->quoteInto('1 = ?', '1');
       // 根据id降序
       $order = 'id desc';
       //  查找记录
       $info = $content->fetchAll($where,$order)->toArray();

       $this->view->news = $info;

    echo $this->view->render('index.phtml');//显示模版
    }
    //添加数据
    function addAction()
    {
     //  获取数据传输方式并把它变成小写,判断是否等于post
     if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
            //使用post的方式接收title,content
      $title = $this->_request->getPost('title');
      $content = $this->_request->getPost('content');
      $addtime = time();

      $news = new Content();
      //  将所有的数据放在一个数组当中
      $data = array(
      'title' =>$title,
      'content' => $content,
      'addtime'=>$addtime
      );
      // 添加数据
      if($news->insert($data)){
       echo'添加数据成功!';
       echo'<a href="'.$this->view->baseUrl.'/index/index/">返回首页</a>';
       unset($data);
      }else{
       echo'添加数据失败!';
       echo'<a href="'.$this->view->baseUrl.'/index/index/">返回首页</a>';
      }
     }
    }
    //  编辑数据
    function editAction()
    {
     //  实例化
     $content = new Content();
     $db = $content->getAdapter();
        //  获取数据传输方式并把它变成小写,判断是否等于post
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            // 通过post方式获取 id,title,content
         $id = $this->_request->getPost('id');
         $title = $this->_request->getPost('title');
         $content = $this->_request->getPost('content');
         // 将数据放在一个数组当中
         $data = array(
         'title'=>$title,
         'content'=>$content,
         );
         //  条件语句 id=
         $where = $db->quoteInto('id = ?',$id);
         //  更新数据
         $content->update($data,$where);
         
         echo "更新数据成功!";
         echo'<a href="'.$this->view->baseUrl.'/index/index/">返回首页</a>';
         unset($data);
        }else{
         $id = $this->_request->getParam('id');

      $this->view->content = $content->fetchAll('id='.$id)->toArray();

   echo $this->view->render('edit.phtml');//显示编辑模板
        }

    }
    //  删除数据
    function deleteAction()
    {
     //  实例化Content类
     $content = new Content();
     //  (Zend frameword)将会自动对修改对数据进行加引号处理,但是这种检查不包括 条件分句,
     //    所以你需要使用该表的zend_db_adapter对象完成该工作.

     $db = $content->getAdapter();
        //  通过get方式来取得id的值
     $id = $this->_request->getParam('id');
     //  sql语句的删除条件
     $where = $db->quoteInto('id = ?',$id);
     //  删除
     $content->delete($where);
     echo "删除成功!";
     echo'<a href="'.$this->view->baseUrl.'/index/index/">返回首页</a>';
    }
    // 文章的具体信息
    function articleAction()
    {
     //实例化
     $content = new Content();
        //  get方式获取id
     $id = $this->_request->getParam('id');
        //  查找记录
     $info = $content->find($id)->toArray();
        //  赋值给模板页面
     $this->view->info = $info;
     echo $this->view->render('article.phtml');
    }
}

model content.php

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值