thinkphp6.0

<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
class Index extends BaseController
{   public function index13(){
        #$query=Db::query("select * from betty13");
        #print_r($query);

        #$execute=Db::execute("insert into user set `phone`='19374581820',`u_name`='小哲' ");
        #$execute=Db::execute("insert into user set `uid`=100, `phone`='19374581820',`u_name`='小叶' ");
        #print_r($execute);

        #$find=Db::table('user')->where('uid','>',1)->select();
        #var_dump($find);

        #$value=Db::table('user')->where('uid',1)->value('u_name');
        #var_dump($value);

        #$col=Db::table('user')->column('u_name');
        #var_dump($col);

        #$data=['phone'=>'19892572123','u_name'=>'fang','uid'=>13];
        #$insert=Db::table('user')->insert($data);
        #var_dump($insert);

        #修改
        $data=['phone'=>'19892572123','u_name'=>'thang','uid'=>13];
        $save=Db::table('user')->save($data);
        var_dump($save);

        
}

 

模板渲染
控制器和模版,它们名字是对应的通过fetch方法,
访问控制器时,就可以渲染模版想使用fetch I方法,
要先use引入View 1.装view模块命令
composer require topthink/think-view
2.改controller模块

应用框架
 use think\facade\View;
#
    public function index()
    {
        return View::fetch();
    }

 

2.新建视图文件文件
 C:\phpstudy_pro\WWW\MyTp6\app\view\index\index.html

 

单应用多模式和应用模式

 

tp6.com/index.php/index/index13  
结果:Array ( [0] => Array ( [uid] => 1 [phone] => 13311111111 [u_name] => apple [sex] => 1 [age] => 18 [status] => 1 ) [1] => Array ( [uid] => 2 [phone] => 13322222222 [u_name] => banana [sex] => 1 [age] => 19 [status] => 1 ) [2] => Array ( [uid] => 3 [phone] => 13333333333 [u_name] => coconut [sex] => 1 [age] => 20 [status] => 1 ) [3] => Array ( [uid] => 4 [phone] => 13344444444 [u_name] => durian [sex] => 1 [age] => 21 [status] => 1 ) )
www.tp6.com/index.php/index/index 结果: View!!!Damn

路由定义文件

路由规则的注册必须在应用的路由定义文件中完成

没有全局路由定义的概念

├─app
├─public
├─route                 路由定义目录
│  ├─route.php          路由定义
│  ├─api.php            路由定义
│  └─...                更多路由定义

如果你使用了多应用模式,
那么路由定义文件需要增加应用子目录,类似于下面:(此配置在6.02新版本失效

├─app
├─public
├─route                 路由定义目录
│  ├─admin              admin应用路由定义目录
│  │ ├─route.php        路由定义文件
│  │ └─...              更多路由定义



#或者推荐
├─admin                 admin应用目录
│  ├─route              路由定义目录
│  │ ├─route.php        路由定义文件
│  │ └─...              更多路由定义

此方法优先级最高

注意

多应用模式下面,如果你开启了自动多应用,路由的规则是指在URL地址的应用名之后的部分,也就是说URL中的应用名是不能省略和改变的,例如你在index应用中定义了路由

 

#多应用模式下面,如果你开启了自动多应用,路由的规则是指在URL地址的应用名之后的部分,也就是说URL中的应用名是不能省略和改变的,例如你在index应用中定义了路由。
Route::rule('hello/:name', 'index/hello');


#在没有开启自动多应用的情况下,URL地址是
http://serverName/index.php/hello/think

#一旦你开启了自动多应用,那么实际的URL地址应该是
http://serverName/index.php/index/hello/think

实验

当route/app.php源码

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Route;

Route::get('think', function () {
    return 'hello,ThinkPHP6!';
});

Route::get('hello/:name', 'index/hello');

访问http://127.0.0.1/MyTp6/public/index.php/hello/aaa页面得到hello aaa

而访问http://127.0.0.1/MyTp6/public/index.php/index/hello/aaa报错

2.route/app.php源码即注释掉Route::get('hello/:name', 'index/hello');为

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Route;

Route::get('think', function () {
    return 'hello,ThinkPHP6!';
});

#Route::get('hello/:name', 'index/hello');

访问http://127.0.0.1/MyTp6/public/index.php/hello/aaa报错

而访问http://127.0.0.1/MyTp6/public/index.php/index/hello/aaa页面得到nkPHP6

 

模板赋值(view:assign)和助手函数(return view())
实现控制器和视图的交互
控制器控制参数,view视图能接受并显示

 

posted @ 2024-04-24 20:36  hacker-dreamer  阅读(2)  评论(0编辑  收藏  举报