认识thinkphp框架

https://www.thinkphp.cn/down.html
版本5.0.24

ThinkPHP是一个免费开源的,快速、简单的面向对象的轻量级PHP开发框架
tp5 WEB部署目录(或者子目录)
├─application 应用目录
│ ├─common 公共模块目录(可以更改)
│ ├─module_name 模块目录
│ │ ├─config.php 模块配置文件
│ │ ├─common.php 模块函数文件
│ │ ├─controller 控制器目录
│ │ ├─model 模型目录
│ │ ├─view 视图目录
│ │ └─ … 更多类库目录
│ │
│ ├─command.php 命令行工具配置文件
│ ├─common.php 公共函数文件
│ ├─config.php 公共配置文件
│ ├─route.php 路由配置文件
│ ├─tags.php 应用行为扩展定义文件
│ └─database.php 数据库配置文件

├─public WEB目录(对外访问目录)
│ ├─index.php 入口文件
│ ├─router.php 快速测试文件
│ └─.htaccess 用于apache的重写

├─thinkphp 框架系统目录
│ ├─lang 语言文件目录
│ ├─library 框架类库目录
│ │ ├─think Think类库包目录
│ │ └─traits 系统Trait目录
│ │
│ ├─tpl 系统模板目录
│ ├─base.php 基础定义文件
│ ├─console.php 控制台入口文件
│ ├─convention.php 框架惯例配置文件
│ ├─helper.php 助手函数文件
│ ├─phpunit.xml phpunit配置文件
│ └─start.php 框架入口文件

├─extend 扩展类库目录
├─runtime 应用的运行时目录(可写,可定制)
├─vendor 第三方类库目录(Composer依赖库)
├─build.php 自动生成定义文件(参考)
├─composer.json composer 定义文件
├─LICENSE.txt 授权说明文件
├─README.md README 文件
├─think 命令行入口文件

打开debug调试模式
将application/config.php中的app_debug设置为true
thinkphp采用了mvc的架构方式,我们先说下它的访问模式,当我们直接访问http://www.tp5024.com:8080/public/
它显示的这样的页面
在这里插入图片描述

这样子默认其实访问到的是
http://www.tp5024.com:8080/public/index.php/index/index/index
访问到的是application中index模块下的index控制器的index方法
在这里插入图片描述
为方法创建模板
在index.php创建一个新的方法hello

<?php
namespace app\index\controller;
use think\Db;
use think \controller;
class Index extends Controller
{

 public function hello($name='thinkphp'){
        $this->assign('name',$name);
        return $this->fetch();


    }
    }

?>

在application/index模块的文件夹下新建一个view文件在里面创建index文件夹在创建hello.html这就是hello方法的模板
在这里插入图片描述
这样直接访问http://www.tp5024.com:8080/public/index.php/index/index/hello
在这里插入图片描述
对参数name赋值为1
http://www.tp5024.com:8080/public/index.php/index/index/hello/name/1

在这里插入图片描述
thinkphp连接查询数据库
thinkphp的数据库配置文件在application/database.php
在这里插入图片描述

<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
class Index extends Controller
{
public function hello($name='thinkphp'){
        $data = Db::name('user')->find();
        $this->assign('result',$data);
        return $this->fetch();


    }
    }

修改模板application/index/view/index/hello.html
在这里插入图片描述
在这里插入图片描述
可以看到查询的数据直接显示了出来

url和路由

在index模块下新建一个HelloWorld控制器
在这里插入图片描述
这里的H和W均为大写
在这里插入图片描述
这种命名形式的控制器需要这样来访问
http://www.tp5024.com:8080/public/index.php/index/hello_world/index
在这里插入图片描述
在这里插入图片描述
路由文件是在application/route.php,我们自定义一个路由
在这里插入图片描述

这里就是当我们直接访问http://www.tp5024.com:8080/public/index.php/hello就能够相当于直接访问index/index/hello,/[:name]$是一个完全限定匹配就是当你在后面传入一个参数,只能匹配到这个name变量

如何在访问中传入参数

 public function hello($name='thinkphp'){
        return $name;
//        $data = Db::name('user')->find();
//        $this->assign('result',$data);
//        return $this->fetch();


    }

像这样的方法如果我们直接访问/public/index.php/index/index/hello那么name的值默认就是thinkphp 如果我们想要给name赋值可以这样 /public/index.php/index/index/hello/name/love通过/加上变量名/加上/值
在这里插入图片描述

如果有多个变量名也是一样

  • 19
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值