1. 程式人生 > >php架構-ThinkPHP架構(一)-TP原理及路徑問題及後臺實現例項(可執行)

php架構-ThinkPHP架構(一)-TP原理及路徑問題及後臺實現例項(可執行)

   本人兩個月前,剛完成基於PHP的研會入口網站,雖然實現了基本功能,但感覺技術有些單薄,便想了一些優化的技術,畢竟專案做的不再多,而在於質量。

   -- 前臺使用了bootstrap框架技術,美化頁面效果很顯著(接下來計劃有時間總結下bootstrap)。

   -- 後臺打算使用ThinkPHP框架技術,這樣可以使整體架構是MVC模式,結構化和模組化專案,並且使頁面的html頁碼和php程式碼分離。

  -- 最後計劃實現頁面的靜態化,方便吸引搜尋引擎爬蟲的曝光率。

  今天就先介紹總結一些TP框架的入門使用,以本人編寫專案為主線介紹,希望也能夠幫助和我一樣剛接觸TP架構的朋友。

  後臺應用TP框架:

  1)路徑問題

         由於TP框架是MVC架構,原理跟smaty模板的一樣,contraller呼叫view下的模板,將模板html頁面替換成php,然後包含到contraller下的控制頁面,並且快取在快取夾cache中,訪問contraller時會自動定位到cache下的快取php檔案。這樣就引出了路徑的問題,模板view下的相對路徑需要些contraller的相對路徑,建議用絕對路徑。
介紹幾個系統常量:
網站根目錄地址       __ROOT__    路徑為根目錄 /
當前路徑下  __URL__     
公共區:  __PUBLIC__     路徑為  /Public/
當前應用入口 __APP__
還可以自己定義路徑變數,方便專案開發。

例子:建議使用絕對路徑代替相對路徑

<link rel="stylesheet" href="__PUBLIC__/css/bootstrap.css"> 代替<link rel="stylesheet" href="../../Public/css/bootstrap.css">

 <img src="__ROOT__/admin/Home/View/Public/images/logo.png"/>代替 <img src="../../../../admin/Home/View/Public/images/logo.png"/>

2)資料庫的連線展示,例子效果如下:

  

   (1)ThinkPHP/Conf/conversation.php中配置資料庫連線引數:

   /* 資料庫設定 */
    'DB_TYPE'               =>  'mysql',     // 資料庫型別
    'DB_HOST'               =>  'localhost', // 伺服器地址
    'DB_NAME'               =>  'yanhui',          // 資料庫名
    'DB_USER'               =>  'root',      // 使用者名稱
    'DB_PWD'                =>  '',          // 密碼
    'DB_PORT'               =>  '',        // 埠

    (2)Contraller中新建控制news頁面NewsContrallor:

<?php
namespace Home\Controller;
use Think\Controller;
class NewsController extends Controller {
    public function index(){
       $user=M('news');
       $this->rows=$user->order('id')->select();
       $this->display();
    }
    public function add(){
       $this->display();
    }
    public function insert(){
       $this->display();
    }
    public function delete(){
       $this->display();
    }
    public function edit(){
       $this->display();
    } public function update(){
       $this->display();
    }
}

(3)View下新建模板頁面News/index.html(用了bootstrap展示前端)

<div class="container">   
    <div class="panel panel-primary">
        <div class="panel-heading">新聞展示</div>
        <div class="panel-body">
            <table class="table table-bordered table-striped">
                <tr>
                    <th>id</th>
                    <th>標題</th>
                    <th>概要</th>
                    <th>上牆</th>
                    <th>時間</th>
                    <th>欄目</th>
                </tr>            
                <volist name='rows' id='row'>
                    <tr>
                        <td>{$row.id}</td>
                        <td>{$row.title}</td>
                        <td>{$row.abstract}</td>
                        <td>{$row.shelf}</td>
                        <td>{$row.regtime|date='Y-m-d',###}</td>
                        <td>{$row.newsclassId}</td>
                    </tr>                
                </volist>
            </table>
        </div>
    </div>
</div>

(根據這個例子,依次實現news模組的增刪改查方法)