1.目录结构:
其中thinkphp子目录是框架核心目录
thinkphp结构:
2.入口文件
默认自带的入口文件位于public/index.php
应用目录为application,其结构:
index模块目录结构:
index为控制器文件;
3.控制器:
找到index模块的index控制器;
找到index模块的index控制器
去把返回值变为helloworld
访问:
看到输出结果!
4.数据的读取:
数据库:
需要在应用的数据库配置文件application/database.php中添加数据库的连接信息如下:
<?phpreturn [ 'type' => 'mysql', // 数据库类型 'hostname' => '127.0.0.1', // 服务器地址 'database' => 'outengcms', // 数据库名'username' => 'root', // 用户名'password' => 'root', // 密码'hostport' => '3306', // 端口'dsn' => '', // 连接dsn'params' => [], // 数据库连接参数 'charset' => 'utf8', // 数据库编码默认采用utf8 'prefix' => 'think_', // 数据库表前缀 'debug' => true, // 数据库调试模式 'deploy' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) 'rw_separate' => false, // 数据库读写是否分离 主从式有效 'master_num' => 1, // 读写分离后 主服务器数量 'slave_no' => '', // 指定从服务器序号 'fields_strict' => true, // 是否严格检查字段是否存在 'resultset_type' => 'array', // 数据集返回类型 array 数组 collection collection对象 'auto_timestamp' => false, // 是否自动写入时间戳字段 'sql_explain' => false, // 是否需要进行sql性能分析];
接下来,我们修改下控制器方法,添加读取数据的代码:
<?php
namespace app\silingling\controller;use think\controller;//use think\db;class index extends controller
{public function _empty($name)
{return $this->fetch('/public/404');
} public function tianjia($code='')
{if(!captcha_check($code)) {$this->error('验证码错误');
}//// echo 111111111;////添加数据库1else { $naa = $_post[naa];$tel = $_post[tel];//留言联系我们db::table('think_shenqing')->data(['naa'=>$naa,'tel'=>$tel])->insert(); $this->success('添加成功','index');
}
// // }
}
控制器写好后,直接修改模版文件,用标签显示就可以了;
5.url访问
thinkphp采用单一入口模式访问应用,对应用的所有请求都定向到应用的入口文件,系统会从url参数中解析当前请求的模块、控制器和操作,下面是一个标准的url访问格式:
http://servername/index.php/模块/控制器/操作
应用下面的子目录称之为模块,模块全部采用小写命名
应用的index模块的index控制器定义如下:
<?php
namespace app\silingling\controller;use think\controller;//use think\db;class index extends controller
{public function _empty($name)
{return $this->fetch('/public/404');
}public function index()
{return $this->fetch('/public/index');
} public function index1()
{ $list=db::name('auth_rule')->where('sort', 55)->select();$this->assign('list',$list);// liucheng$list3 = db::name('article')->where('writer',22)->select();$this->assign('list3',$list3);//chaxun$list211 = db::name('haoma')->where('code'>0)->select();$this->assign('list211',$list211);return $this->fetch('/public/index1');
}
}
如果我们直接访问入口文件index,因为我们没有指定url,所以系统会访问默认模块(index)下面的默认控制器(index)的默认操作方法(index),
http://localhost/index.php
http://localhost/index.php/index/index/index
这两个连接等效!
应用的index模块的index控制器定义如下:
<?php
namespace app\lianxi\controller;use think\controller;use think\db;class index extends controller
{public function _empty($name)
{return $this->fetch('/public/404');
} public function index(){return 'index';
}public function hello($name = 'world'){return 'hello,' . $name . '!';
}
}
如果我们直接访问入口文件的话,默认走的是index方法,
如果要访问控制器的hello方法,则需要使用完整的url地址
输出的是:
由于name参数为可选参数,连接这样输:
http://localhost/index.php/lianxi/index/hello/name/xuanxuan
输出:
6.模板渲染输出:
输出当前模块下的index模板:
// 指定模板输出
$this->display('index');
输出user模块下面的read模板:
$this->display('user:read');
输出模板时指定编码和类型:
// 表示输出xml页面类型(注意:这里可以输出网站地图sitemap.xml哦~~)
$this->display('read', 'utf-8', 'text/xml');
总结一下,thinkphp的模板渲染可以设置编码类型及输出文件的类型
以上就是th5框架的介绍和url访问的详细内容。