zendframeworkを使用したPHPプログラミング例

WBOY
リリース: 2016-07-30 13:29:42
オリジナル
1062 人が閲覧しました

この記事は、「PHP Top Framework zendframe の実践開発」の第 4 章の内容を参照し、完全に実装しています...

まず、使用する CSS ファイルをダウンロードします: http://download.csdn.net/download/unityoxb/ 4058802

解凍後、デフォルトファイルと共通ファイルを 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 を開き、ソース mysql.sql を使用してテーブル構造を作成します

2 . application.ini ファイルを設定します (hahacom/applicaton/configs)

主に mysql に接続するように zend Framework を設定します

[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. 記事表示モデルを作成します主にデータを保存し、モデルは同様の 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コントローラーを作成します

コマンドを実行します: zf createコントローラーニュースは自動的に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コントローラーページとzf createアクション詳細ページ

は自動的に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']."</h3>";
echo $this->News[0]['body'];
//echo $this->name;
if($this->News)
{
   /*echo "<ul>";
  // print_r($this->News);
   foreach($this->News as $val)
   {
      echo "<li>"."<u>".$val['title']."</u>"."</li>";
   }
   echo "</ul>";
   */
   echo "<ul class = &#39;listNews&#39;>";
   echo $this->partialLoop('row_pages.phtml', $this->News);
   echo "</ul>";
}
?>
ログイン後にコピー

/views/scripts/row_pages.phtml

<li>
   <a href = "/page/detail/id/<?php echo $this->id; ?>"><?php echo $this->title; ?></a>
   发表时间: <?php echo date(&#39;Y-m-d&#39;, $this->createtime); ?>
</li>
ログイン後にコピー

/views/scripts/page/detailを作成します。 phtml

<?php
   echo "<h2>".$this->page['title']."</h2>";
   echo "发表:".date('Y-m-d', $this->page['createtime'])."";
   echo "<hr/>";
   echo $this->page['body'];
?>
ログイン後にコピー

実行中のスクリーンショット:


リンクをクリックしてください:


著作権表示: この記事はブロガーによるオリジナルの記事であり、ブロガーの許可なく複製することはできません。

以上、zendframeworkを使ったPHPプログラミング例を内容も含めて紹介しましたが、PHPチュートリアルに興味のある友人の参考になれば幸いです。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!