ThinkPHP learning 2# - 第一個ThinkPHP 例子____PHP

來源:互聯網
上載者:User

測試環境:Window XP sp3

               XAMPP 1.7.4(Apache 2.2.17、MySql 5.5.8、PHP 5.3.5)

               ThinkPHP 2.1

                PHPeclipse

 

使用ThinkPHP建立應用的一般開發流程是: 建立資料庫和資料表;(沒有資料庫操作可略過) 項目命名並建立項目入口檔案; 完成項目配置;(無需額外配置可以忽略) 建立控制器類; 建立模型類;(如果只是簡單的模型類可以不必建立) 建立模板檔案; 運行和調試。

1、建立資料庫和資料表

 

     資料庫名稱demo:

 

     CREATE TABLE `think_demo` (
          `id` int(11) unsigned NOT NULL auto_increment,
          `title` varchar(255) NOT NULL default '',
          `content` longtext NOT NULL,
          PRIMARY KEY (`id`)
     ) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;

 

2、建立項目

     在apache DocumentRoot 所在目錄下建立應用(註:這雷根據測試環境會有不同),項目名Myapp,同時將ThinkPHP核心拷貝到Myapp下,接著建立入口檔案index.php

<?php /* * Created on 2011-6-10 * * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */ // 定義ThinkPHP架構路徂 define('THINK_PATH', './ThinkPHP/'); //定義項目名稱和路徂 define('APP_NAME', 'Myapp'); define('APP_PATH', '.'); // 載入架構入口檔案 require(THINK_PATH."/ThinkPHP.php"); //執行個體化一個網站應用程式執行個體 App::run(); ?>

 

訪問入口檔案,自動產生項目目錄。

http://localhost:8081/Myapp/index.php

成功後,可看到歡迎頁面:

 

 

3、項目配置

     在自動產生的目錄下面,已經建立了一個空的項目設定檔,Conf/config.php

開啟增加項目配置資訊:

<?php return array( //'配置項'=>'配置值' 'APP_DEBUG' => true, // 開啟偵錯模式 'DB_TYPE'=> 'mysql', // 資料庫類型 'DB_HOST'=> 'localhost', // 資料庫朋務器地址 'DB_NAME'=>'demo', // 資料庫名稱 'DB_USER'=>'root', // 資料庫使用者名稱 'DB_PWD'=>'', // 資料庫密碼 'DB_PORT'=>'3306', // 資料庫連接埠 'DB_PREFIX'=>'think_', // 資料表首碼 ); ?>

 

4、增加商務邏輯(增加為例)

 

    Lib/Action下,自動產生的IndexAction.class.php(控制器),注釋(或者刪除)掉當前的index方法,添加新的insert、index方法:

    // 資料寫入操作 public function insert() { $Demo = new Model('Demo'); // 執行個體化模型類 $Demo->Create(); // 建立資料對象 $result = $Demo->add(); // 寫入資料庫 $this->redirect('index'); // 成功後重新導向刡index操作頁面 } // 資料查詬操作 public function index() { $Demo = new Model('Demo'); // 執行個體化模型類 $list = $Demo->select(); // 查詢資料 $this->assign('list', $list); // 模板發量賦值 $this->display(); // 輸出模板 }

 

5、模板定義

    Tpl/default下建立index 目錄,用於存放index模組的模板檔案,例子只需要為index操作定義模板檔案即可(insert是後台操作):

     index.html

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>ThinkPHP Demo</title>
</head>
<body>
<!--資料新增表單-->
<form method="post" action="__URL__/insert">
    標題:<input type="text" name="title"><br />
    內容:<textarea name="content" rows="5" cols="25"></textarea><br />
  <input type="submit" value="新增資料">
</form>

<!--很環輸出查詬絀果資料集-->
<volist name='list' id='vo'>
編號:{$vo.id}
<br />
標題: {$vo.title}
<br />
內容: {$vo.content}
<hr>
</volist>
</body>
</html>

 

6、測試

 

      運行httP://localhost:8081/Myapp/

 

     

 

     由於在config裡開啟了debug模式,可以看到trace資訊。

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.