ThinkPHP学习笔记

来源:互联网 发布:神经网络算法是什么 编辑:程序博客网 时间:2024/06/05 06:19

 

ThinkPHP

搭建开发环境

 Eclipse 上写PHP  把工作空间部署到 C:\xampp\htdocs\这样修改代码后即可在浏览器中测试

 XAMPP 集成包 当中包含Apache MySQLphpMyAdmin

 把项目部署到C:\xampp\htdocs\目录下

 

MVC设计模式

Model -> 数据库操作模型 把数据库中的数据封装成对象模型供控制器使用

View -> 视图页面  供用户进行交互操作

Controller ->控制器 MVC模式的中心

 

ThinkPHP采用单一入口文件方式进入系统。

通过单入口传入参数,控制器返回指定页面。

ThinkPHP的包结构如下

  1. ├─index.php     项目入口文件
  2. ├─Common 项目公共文件目录
  3. ├─Conf 项目配置目录
  4. ├─Lang 项目语言目录
  5. ├─Lib 项目类库目录
  6. │  ├─Action Action类库目录
  7. │  ├─Behavior 行为类库目录
  8. │  ├─Model 模型类库目录
  9. │  └─Widget Widget类库目录
  10. ├─Runtime 项目运行时目录
  11. │  ├─Cache 模板缓存目录
  12. │  ├─Data 数据缓存目录
  13. │  ├─Logs 日志文件目录
  14. │  └─Temp 临时缓存目录
  15. └─Tpl 项目模板目录

 

其中,Lib下的Action文件夹中放置Controller,并用Action后缀名来区分。

一个控制器相当于一个类,并且该类继承Action类。

Tpl 项目模板目录中放置View 以HTML文件来显示。

导入ThinkPHP包

1、在项目中新建index.php文件

2、把ThinkPHP3.1.3_full 中的ThinkPHP文件夹复制到项目中

3、在index.php中输入以下代码

<?php

//定义项目名称

define('APP_NAME','Vmall');

//定义项目路径

define('APP_PATH','./Vmall/');

define('APP_DEBUG',true);

//加载框架入文件

require './ThinkPHP/ThinkPHP.php';

?>

4、在浏览器中输入http://localhost/“项目名”/  出现欢迎使用ThinkPHP页面。项目中出现相应的文件Vmall  即可查看ThinkPHP的框架结构。

 

 

 

 

数据库操作

 

首先在Conf/config.php 中配置数据库的相关信息。

 

Conf/config.php

<?php

return array(

   //'配置项'=>'配置值'

        'DB_TYPE'              => 'mysql',    // 数据库类型

        'DB_HOST'              => 'localhost',//服务器地址

        'DB_NAME'              => 'test',         // 数据库名

        'DB_USER'              => 'root',     // 用户名

        'DB_PWD'               => '',         // 密码

        'DB_PORT'              => '',       // 端口

        'DB_PREFIX'            => 'wyb_',   // 数据库表前缀

);

?>

 

 

然后在Lib/Action/IndexAction.class.php 中定义相关的函数操作数据库

Lib/Action/IndexAction.class.php

<?php

// 本类由系统自动生成,仅供测试用途

class IndexActionextends Action {

   public functionindex(){

        $User = M('User');// 实例化User数据模型

        $this->user = $User->select();

        $this->display();

   }

   //显示表中的数据

   public functionshowAll(){

       

   }

   //把表单数据插入数据库

   public functioninsert(){

        $MyModel = M('user');//实例化User数据库模型 表的名字叫wyb_user已经在conf中定义了数据库前缀

        $MyModel ->user_name ='谷歌';

        $MyModel ->add();

        $this->test = $MyModel ->select();//testindex.html当中volist元素的name至此volist获取到了查询到的数据

        $this->display();

   }

}

最后在Tpl/Index/insert.html中设计相应的网页进行显示

Tpl/Index/insert.html

  <html>

    <head>

       <title>Insert Data</title>

    </head>

    <body>

        <volistname="test"id="vo">

        {$vo.user_id}--{$vo.user_name}<br/>

        <!--

          user_iduser_name为数据库中的字段

       -->

        </volist>

    </body>

   </html>

   <!--

   HTML注释

    -->

 

通过访问网址http://localhost/mytest1/?m=Index&a=insert

其中m=Index代表Lib/Action目录下的IndexAction.class.php 中的Index 类

     a=insert代表Index类中的insert方法

利用这种方式访问网站将得到需要定义相应的insert.html网页。

得到的显示结果如下

2--惠普
3--戴尔
1--联想
4--苹果
5--谷歌

 

 

M函数的用法: M(‘数据库表名’) 

获取表的主键:$pk = $Model->getPk();

0 0
原创粉丝点击