ThinkPHP架构(一)-TP原理及路径问题及后台实现实例

     一直用CSDN的博客,由于域名当时注册写的不合适,现在想来博客园写博客,以后要坚持写啦,记录自己的技术学习路程

     本人两个月前,刚完成基于PHP的研会门户网站,虽然实现了基本功能,但感觉技术有些单薄,便想了一些优化的技术,毕竟项目做的不再多,而在于质量。

   -- 前台使用了bootstrap框架技术,美化页面效果很显著(接下来计划有时间总结下bootstrap);并且应用HTML语义化文章结构,便于搜索引擎查找。

   -- 后台打算使用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模块的增删改查方法)

posted @ 2016-03-10 14:51  rongyux  阅读(6134)  评论(0编辑  收藏  举报