thinkphp基础
来源:互联网 发布:淘宝宝贝优化 编辑:程序博客网 时间:2024/05/17 04:05
1. 写了一个test.php文件
// 定义应用目录为appsdefine('APP_PATH', __DIR__ . '/apps/');// 加载框架引导文件require __DIR__ . '/think/start.php';
直接访问显示模块不存在。
未解决。
该文件是用来修改入口文件的。
2. 首先将tp5的欢迎页面改成helloworld。
在appication--index--controller--index.php中
将return 的内容改为hellowolrd.
public function index()
{
return 'helloworld';
}
结果:欢迎页面改为hellowolrd
3. 修改或者添加index类控制器中的方法
public function test()
{
return '测试';
}
调用该方法的路径为 http://localhost/tp/public/index.php/index/index/test
结果 :页面显示 测试二字
另:测试方法的修饰符只有为public时,才能通过url访问到。如果修饰符为protetced和private,页面显示方法不存在。
4. 添加模板文件
1)在apllication--index增加view--index目录,并将模板文件:hello.php写在该目录下。
<html><head><title>hello {$name}</title></head><body> hello, {$name}!</body></html>
2)在index类控制器增加hello方法,并且要在开头增加use think/Controller
<?phpnamespace app\index\controller;use think\Controller;class Index extends Controller{ public function hello($name = 'thinkphp') { $this->assign('name', $name); return $this->fetch(); }}
结果 :通过访问http://localhost/tp/public/index.php/index/index/hello
页面显示 hello,thinkphp!
另:use think\Controller相当于class index extends think\Controller
注意,Index
控制器类继承了 think\Controller
类之后,
我们可以直接使用封装好的assign
和fetch
方法进行模板变量赋值和渲染输出。
5. 从数据库中读取数据
1)修改数据库的配置文件apliication/database.php 将用户名,密码,数据表前缀,端口号,数据库名修改成具体内容
return [ // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => '127.0.0.1', // 数据库名 'database' => 'demo', // 数据库用户名 'username' => 'root', // 数据库密码 'password' => '', // 数据库连接端口 'hostport' => '', // 数据库连接参数 'params' => [], // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库表前缀 'prefix' => 'think_', // 数据库调试模式 'debug' => true,];
2)修改模板文件hello.html<html><head><title></title></head><body>{$result.id}--{$result.data}</body></html>
3)修改index类控制器中的index方法
<?phpnamespace app\index\controller;use think\Controller;use think\Db;class Index extends Controller{ public function index() { $data = Db::name('data')->find(); $this->assign('result', $data); return $this->fetch(); }}
访问 http://localhost/tp/public/index/index/hello.html
遇到的问题:在view/index目录下新建了一个result.html模板文件,在访问http://localhost/tp/public/index.php/index/index/result时显示方法参数错误。
将hello.php文件的内容修改成上述内容,并访问http://localhost/tp/public/index.php/index/index/hello则显示了正确的内容
一个index类只有一个模板文件吗????
- thinkphp 基础
- ThinkPHP 基础
- thinkphp基础
- ThinkPHP基础
- thinkphp基础
- thinkphp基础
- ThinkPHP基础
- ThinkPHP数据库基础配置
- 2013.8.1 Thinkphp基础
- ThinkPHP框架基础回顾
- ThinkPHP简单的基础
- PHP基础及ThinkPHP
- thinkphp 3.2 基础
- ThinkPHP快速入门1-基础
- thinkPHP入门(一):基础
- thinkPHP入门(二):基础
- Thinkphp 3.2.3 开发基础
- 关于thinkphp的基础学习(一)
- 变量作用域
- 【框架】Imager-loader_1.9.4图片下载框架
- Python 实现决策树 ID3 C4.5 悲观剪枝
- const在函数前与函数后的区别
- Redis从无到有
- thinkphp基础
- 关于int *a; int &a; int & *a; int * &a
- java浅说(2):文件输出流FileOutputStream
- 我的第一个CSDN博客
- 第一次自由地上网搜索东西
- 谭浩强C程序设计第三版习题8.5
- 1016 Mice and Rice
- javascript 获得请求的路径BASEURL
- jQuery源码分析之$.extend() --静态扩展