thinkphp基础

1.    写了一个test.php文件

// 定义应用目录为apps
define('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

<?php
namespace 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类之后,

我们可以直接使用封装好的assignfetch方法进行模板变量赋值和渲染输出。

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方法

<?php
namespace 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类只有一个模板文件吗????

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值