php利用zendframework编程实例

WBOY
풀어 주다: 2016-07-30 13:29:42
원래의
1061명이 탐색했습니다.

本文章参考《php顶级框架zendframe开发实战》第四章内容,并完整实现...

首先将用到的css文件下载下来:http://download.csdn.net/download/unityoxb/4058802

解压后把default和common两个文件复制到public/skins目录下;

1、用到的数据库文件mysql.sql

create table if not exists `core_pages`(
   `id` int(10) unsigned not null auto_increment comment '页面唯一ID',
   `cid` int(10) unsigned not null default '0' comment '分类ID',
   `uid` int(10) unsigned not null default '0' comment '用户ID',
   `title` varchar(255) not null comment '页面标题',
   `body` text not null comment '内容',
   `status` tinyint(4) not null default '1' comment '是否发布',
   `createtime` int(11) not null default '0' comment '创建页面时间',
   `updatetime` int(11) not null default '0' comment '修改页面时间',
   `comment` tinyint(4) not null default '0' comment '页面是否评论功能',
   `start` tinyint(4) not null default '0' comment '页面级别',
   `top` tinyint(4) not null default '0' comment '置顶',
   primary key (`id`)
)ENGINE=InnoDB default charset=utf8;
로그인 후 복사

打开mysql,使用source mysql.sql即可创建表结构

2、配置application.ini文件(hahacom/applicaton/configs)

主要配置zend Framework连接mysql

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = "root"
resources.db.params.dbname = "test" --这是数据名称
resources.db.isDefaultTableAdapter = "TRUE"
resources.db.params.driver_options.1002 = "SET NAMES UTF8;"
로그인 후 복사

3、public/index.php

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : '<strong>development</strong>')); //修改成测试环境
로그인 후 복사

4、创建文章展示model(model主要存储数据模型类似javabean,或者从数据库获取数据并保存在内存里)

执行命令:zf create model page 

会自动生成一个models/Page.php文件

<?php class Application_Model_Page
{
   protected $_name = &#39;core_pages&#39;;
   public $result;   

   public function getPage($where = array())
   {
      $db = Zend_Db_Table::getDefaultAdapter();
     // $db = $this->getAdapter();
      $select = $db->select();
      /*if($where != null)
       {
          //$select->where(' star = ? ', $where);
          //$sql = $db->quoteInto("select * from `core_pages` where `star`= ?", $where);
          //$result = $db->query($sql);
          $select->from('core_pages','*')->where('star = ?', $where)->limit(1);
       }*/
       $select->from('core_pages','*');
       if(count($where)>0)
       {
          foreach($where as $key=>$value)
             $select->where($key.' = ?',$value);
       }
      //$row = $result->fetch();
      $row = $db->fetchAll($select);
      if($row)
      {
         return $row;
      }
      else
      {
         echo "=================";
         return null;
      }
   }

   public function getPages($where = null)
   {
       $db = Zend_Db_Table::getDefaultAdapter();
       if(is_numeric($where))
       {
           //$row = $db->find($where)->current();
          $select = $db->select();
          $select->from('core_pages','*');
          $select->where('id = ?', $where);
          $row = $db->fetchRow($select);
       }
       if(is_array($where) && count($where)>0)
       {
          
          $select = $db->select();
          $select->from('core_pages','*');
          foreach($where as $key=>$value){
              $select->where($key.'=?', $value);
          }
          $row = $db->fetchAll($select);
       }
      if($row)
      {
         return $row;
      }
      else
      {
         echo "=================";
         return null;
      }

   }
}

?>
로그인 후 복사

5、创建controller

执行命令: zf create controller news   会自动生成controllers/NewsController.php

<?php class NewsController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
        $modelPage = new Application_Model_Page();
        //$star = 1;
        $where = array(&#39;top&#39;=>1, 'comment'=>1);
        $newsStar = $modelPage->getPage($where);
        //print_r($newsStar);
        $this->view->News = $newsStar;
        //$this->view->name = "hahaha"; 
    }


}
로그인 후 복사

执行命令: zf create controller page 和 zf create action detail page

会自动生成 controllers/PageController.php

<?php class PageController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function detailAction()
    {
        // action body
        $id = $this->_request->getParam('id');
        $modelPage = new Application_Model_Page($id);
		//if($modelPage == null)
		  //print_r('==============================');
		//print_r($id);
		//print_r($modelPage);
        $page = $modelPage->getPages($id);
        $this->view->page = $page;
    }


}
로그인 후 복사

5、接下来创建视图文件

/views/scripts/news/index.phtml

<?php echo "<h3>".$this->News[0]['title']."";
echo $this->News[0]['body'];
//echo $this->name;
if($this->News)
{
   /*echo "
로그인 후 복사
    "; // print_r($this->News); foreach($this->News as $val) { echo "
  • "."".$val['title'].""."
  • "; } echo "
"; */ echo "
    "; echo $this->partialLoop('row_pages.phtml', $this->News); echo "
"; } ?>
/views/scripts/row_pages.phtml

로그인 후 복사
  • title; ?> 发表时间: createtime); ?>

  • /views/scripts/page/detail.phtml

    <?php echo "<h2>".$this->page['title']."";
       echo "发表:".date('Y-m-d', $this->page['createtime'])."";
       echo "<hr>";
       echo $this->page['body'];
    ?>
    
    로그인 후 복사

    运行截图:


    点击链接:


    版权声明:本文为博主原创文章,未经博主允许不得转载。

    以上就介绍了php利用zendframework编程实例,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

    관련 라벨:
    원천:php.cn
    본 웹사이트의 성명
    본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
    인기 튜토리얼
    더>
    최신 다운로드
    더>
    웹 효과
    웹사이트 소스 코드
    웹사이트 자료
    프론트엔드 템플릿
    회사 소개 부인 성명 Sitemap
    PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!