ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 导入自定义的配置如 ``` // Conf/config.php // 自动加载 Conf/user.php 和 Conf/ email.php return array( 'LOAD_EXT_CONFIG' => 'user,email', ); ``` ## RESTFul APi 接口 [参考文档](https://www.kancloud.cn/manual/thinkphp/1879) >如果某控制器为纯接口形式,则也可继承该方法,而无需设置 在模块名/userController 控制器中 创建继承RestController的方法 ``` class UserController extends RestController{ protected $allowMethod = array('get','post','put','delete'); // REST允许的请求类型列表 protected $allowType = array('html','xml','json'); // REST允许请求的资源类型列表 Public function index(){ switch($this->_method){ case 'get': //todo $this->response(array('name'=>'cpj','age'=>12),'json',200); break; case 'put': //todo $this->response(array('name'=>'cpj','age'=>12),'json',200); break; case 'post': //todo $this->response(array('msg'=>'提交成功'),'json',200); break; case 'delete': //todo $this->response(array('msg'=>'删除成功'),'json',200); break; } } } ``` 在data/Conf/config.php 中添加 路由映射 ``` 'URL_ROUTER_ON'=>true, 'URL_ROUTE_RULES'=>[ 'imapp/dept'=>array('api/dept/dept_list_redis'), ], ``` 访问 `delete` 形式方法 `http://www.mapi.com/user/2` 则提示删除成功 ## 生成静态缓存 > [查考文档](https://www.kancloud.cn/manual/thinkphp/1839) 在模块或commom /Conf/config.php中配额, ``` return array( 'HTML_CACHE_ON' => true, // 开启静态缓存 'HTML_CACHE_TIME' => 60, // 全局静态缓存有效期(秒) 'HTML_FILE_SUFFIX' => '.html', // 设置静态缓存文件后缀 'HTML_CACHE_RULES' => array( // 定义静态缓存规则 '*'=>array('{:module}/{:controller}/{:action}/{$_SERVER.REQUEST_URI|md5}'),//生成的文件路径,只要注意同样一个页面生成的路径一样即可 ) ); ``` 生成静态文件后,如果存在静态文件,则提前与调用操作方法 ``` public function show(){ echo "hello word"; //存在静态缓存,则hello word 不会输出 //todo $this->display(); } ``` ## 生成Lite 文件,提升网站性能 1. 在入口文件 `index.php` `define('BUILD_LITE_FILE',true);` 2. 替换框架入口文件 ``` require './ThinkPHP/ThinkPHP.php'; // 改成 require 'Application/Runtime/lite.php'; //即在index.php 中引入lite 文件即可. ``` 3. 通过 `index.php` 访问即可 ## 使用 trace ``` 'SHOW_PAGE_TRACE' =>true, //开启 //输出trace trace($_GET,'用户信息'); //显示 用户信息:Array ( [p_reload] => 1 [reload_time] => 1524811491619 [bookcate] => 1 ) ``` ## 表单令牌 > 防止表单的重复提交 在`模块名/Conf/tags.php`中 ``` return array( // 添加下面一行定义即可 'view_filter' => array('Behavior\TokenBuild'), // 如果是3.2.1以上版本 需要改成 // 'view_filter' => array('Behavior\TokenBuildBehavior'), ); ``` 添加配置参数 `'TOKEN_ON' => true, // 是否开启令牌验证 默认关闭` 验证 ``` $User = D("User"); // 生成模型类的验证 if (!$User->create($_POST)){ // 令牌验证错误 } ``` ## Widget扩展 > 常用于需要在模版的foreach 循环中在进行复杂操作 ``` //创建home/widget/DemoWidget.class.php namespace Home\Widget; use Think\Controller; class DemoWidget extends Controller{ public function menu($param1,$param2){ //方式1. 直接删输出 return [$param1, $param2,]; //只能是数组索引才会被list //方式2.支持模版 $menu = M('Cate')->getField('id,title'); $this->assign('menu',$menu); $this->display('Cate:menu'); // Cate/menu.html /* <foreach name="menu" item="title"> {$key}:{$title} </foreach> */ } } //在home/view/index/index.html中 <php> list($name,$param) = W('demo/menu',[['name'=>'cpj'],['age'=>23,'user'=>'hello']]); //w函数数组的形式传参 dump($name); dump($param); </php> ``` ## cli模式(命令行模式) 推荐使用绝对路径的形式 在`index.php`中添加`__DIR__` ``` define('APP_PATH',__DIR__.'/../Application/'); // 引入ThinkPHP入口文件 require __DIR__.'/../ThinkPHP/ThinkPHP.php'; ``` 1. 无参运行 `php index.php cli/index/demo` 2. 有参数运行-参数可写在方法的参数中 `php index.php statis/index/demo/name/cpj/age/12` ## 子域名部署 ``` 'APP_SUB_DOMAIN_DEPLOY' => 1, // 开启子域名配置 'APP_SUB_DOMAIN_RULES' => array( 'admin' => 'Admin', //admin.demo.com 'test' => 'Test', //test.demo.com ), ``` ## block 模板继承 > [手册](https://www.kancloud.cn/manual/thinkphp/1800) 基础模板 ```html <head> <meta charset="utf-8"> <block name="style"></block> </head> <html> <body> <div class="main-content"> <block name="main">文章内容(这段注释会被覆盖掉) </block> </div> <block name="script"></block> </body> <html/> ``` 继承 ```html <extend name="Public/base" /> <block name="style"> <!--针对这个页面的css,link等--> </block> <block name="main"> <!--针对这个页面的内容--> </block> <block name="script"> <!--写正对这个页面的 script--> </block> ``` ## 概念D() 函数,使 IDE识别 Model 的方法 baseModel.class.php ``` class baseModel { protected static $_single; public static function DD() { $className = get_called_class(); if(!class_exists($className)){ return new \Think\Model(); } $className = substr($className,strripos($className,'\\')+1,-5); $formatStr = preg_replace("/([A-Z])/", ",\\1", $className); $wordList = explode(',', $formatStr); array_shift($wordList); $tableName = ''; foreach($wordList as $word){ $tableName .= strtolower($word).'_'; } $tableName = trim($tableName,'_'); if(!isset(self::$_single[$tableName]) || empty(self::$_single[$tableName])){ $obj = self::$_single[$tableName] = new static(); }else{ $obj = self::$_single[$tableName]; } if(empty(self::$_single[$tableName]->tableName)){ //新代码不会再model中定义tableName,所以要在这里定义,以便im_app使用 self::$_single[$tableName]->tableName = $tableName; } return $obj; } } ``` 新的model 继承 baseModel,在其他控制器中使用 ``` UserModel::DD(); ```