• 认识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

    
    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();
    
    
        }
        }
    
    ?>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在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
    在这里插入图片描述

    
    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();
    
    
        }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    修改模板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();
    
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

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

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

  • 相关阅读:
    讲解用Python处理Excel表格
    javaweb JAVA JSP聊天室程序源码(局域网聊天系统 即时通讯)网页聊天系统
    3分钟带你认识腾讯云服务器CVM_一看就懂
    项目管理的四大模型,PM必懂的事半功倍模型!
    在Python中调用imageJ开发
    微信小程序(小程序入门)
    排序算法总结
    焦炉加热系统简述
    第2章丨IRIS Global 结构
    kubernetes问题(一)-探究Pod被驱逐的原因及解决方法
  • 原文地址:https://blog.csdn.net/qq_42307546/article/details/127723119