ThinkCMF 产品开发小结 路由route 模板 nginx ssl 数据库

1 路由重点,默认是混合模式 pathinfo开启 需要使用$this->request->param(“传参”)

2.data目录下config中route.php 是最核心路由 可以存放return配置,应用路由即在当天目录下创建app.php

3.当前网址 {$Request.scheme}://{$Request.host}/

data/config/route.php 【一般产品前端采用强制模式(前端模板必须使用url函数产生链接)】

<?php
use think\facade\Route;

return [
    // 关闭自动路由(核心:禁止通过 模块/控制器/方法 直接访问)
    'auto_route'          => false,
    // 关闭控制器后缀(可选,根据你的代码规范调整)
    'controller_suffix'   => false,
    // 关闭URL路由匹配的大小写转换(可选,增强严格性)
    'url_convert'         => false,
    // 关闭默认的路由延迟解析(确保路由规则优先生效)
    'delay_route'         => false,
];

data/config/app.php 可以存放前端模板中的路由【可以防止出问题】

<?php
use think\facade\Route;


Route::get('/func/set', 'demo/user/get_show');
Route::get('/func/gp', 'demo/user/list');

#配置
Route::get('/func/save', 'myapp/user/show_done');
Route::get('/func/del', 'myapp/user/del');
Route::get('/func/add', 'myapp/user/add_gupiao');

Route::get('/login', 'user/login/index');
Route::get('/register', 'user/register/index');
Route::get('/password', 'user/login/findPassword');
Route::get('/logout', 'user/index/logout');



Route::get('/repassword', 'user/profile/password');
Route::get('/edit', 'user/profile/edit');
Route::get('/avatar', 'user/profile/avatar');




Route::get('/rss', 'myapp/gu/index');
Route::get('/weather', 'myapp/gu/weather');

#上面路由模式
#下面路由 指定主页 创建短链

Route::rule('/', 'demo/index/index', 'GET')->name('home');
Route::rule('s/:short', 'myapp/gu/short', 'GET')->pattern(['short' => '\w+']);

后台路由没有关系 ,后台用于编辑,可以使用后台加密,双密码,相对好一点,或者直接不用后台,使用另一个网址应用连接数据库实现隔离。

模板

 <include file=”public@head” /> 在模板里面可以引入多个HTML上面的是在应用目录的public/head.html文件,比如你的应用是demo(default/demo/下面),那么这个就是在 default/public/head.html 非应用下的public(不是 default/demo/public)

Nginx SSL

使用lnmp ssl add添加SSL 用自动签注的SSL需要修改nginx的conf

原来的Nginx添加(其中root /home/wwwroot/xxx.com; 不是thinkphp的public 切记)

location ^~ /.well-known/acme-challenge/ {
	    allow all;
	    default_type text/plain;
	    root /home/wwwroot/xxx.com;
	}

然后再:
lnmp restart
lnmp ssl add

然后完成后SSL后请设置,SSL的端口(其中root /home/wwwroot/xxx.com; 请修改成thinkphp的root /home/wwwroot/xxx.com/public; 不然ssl无法访问)

server
    {
        listen 443 ssl http2;
        #listen [::]:443 ssl http2;
        server_name acg.wang ;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/xxx.com/public;

最后 lnmp restart

数据库

数据库关联单条数据超过3000 条数据 可能会触发PHP memory错误直接502 默认PHP是128MB,如果实在有多个3000条数据关联排序请调整到256MB或者512MB,但是还是请不要关联超过3000条数据再排序。如下面的代码是有问题的:

    public function codehistroy(){
         return $this->hasOne(HistroyModel::class,"code",'code')->order('create_time','desc');
     }
#此处模型 关联排序会极大消耗PHP内存

在功能Model,即非创建关联表的模型(内部调用表的模型)直接初始化!
class Xaa{ 无需 class Xaa extends Model{

发表在 None | 留下评论

路由路由thinkcmf下route设置

php.ini里面需要把path_info打开(800行左右)
cgi.fix_pathinfo = 1

TAG:
$this->request->param(‘short’) 才能/demo/gu/short/short/xx (或/demo/gu/short?short=xx)获取信息
$this->request->get(‘short’) 只能/demo/gu/short?short=xx 获取信息

在/data/route/route.php 是全局路由

<?php
use think\facade\Route;


Route::rule('s/:short', 'demo/gu/short', 'GET')->pattern(['short' => '\w+']);

#此处将/s/xx 转到/demo/gu/short/short/xx (或/demo/gu/short?short=xx)

return [
    // 是否开启路由延迟解析
    'url_lazy_route'        => true,
    
    // 默认的路由变量规则
    'default_route_pattern' => '[\w\.]+',
    
    // 是否开启强制路由
    'url_route_must'        => false,
    
    // 合并路由规则
    'route_rule_merge'      => false,
    
    // 路由是否完全匹配
    'route_complete_match'  => false,
    
    // 去除斜杠
    'remove_slash'          => false,
];

发表在 None | 留下评论

PHP多维排序 PHP>7 unsort()

$pre_show=[
    ["value"=>8,"name"=>"name","cn"=>"股票名称"],
    ["value"=>2,"name"=>"code","cn"=>"股票代码"],
    ["value"=>7,"name"=>"price","cn"=>"股票价格"],
    ["value"=>4,"name"=>"xadd","cn"=>"涨跌"],
    ["value"=>5,"name"=>"add_pre","cn"=>"涨幅"],
    ["value"=>6,"name"=>"hand","cn"=>"换手数"],
    ["value"=>3,"name"=>"money","cn"=>"成交额"],
    ["value"=>1,"name"=>"percent","cn"=>"换手率"],
    ["value"=>9,"name"=>"all_money","cn"=>"市值"]
];

// 按 value 升序排序(推荐:PHP7+ 简洁写法)
usort($pre_show, function($a, $b) {
    return $a['value'] - $b['value']; // 升序:a-b,降序:b-a
});

// 打印排序结果(验证)
var_dump($pre_show);
发表在 None | 留下评论

thinkphp thinkcmf 惰加载 with hasOne hasMany

$info=$xModel->select()
$info=$xModel->with(“bxx”)->select()
区别在于在$info在返回后是否使用bxx,如果不使用的化 不用with的将不会关联 即惰加载,减少数据库的IO

TAG:关联可以采用非主键关联


hasOne 关联单数组

默认hasOne关联的是第一条数据,即最老的数据

hasMany关联数组-数组

A: return $this->hasOne(HistroyModel::class,”code”,’code’)->order(‘create_time’,’desc’);
B: return $this->hasMany(HistroyModel::class,”code”,’code’)->order(‘create_time’,’desc’)->limit(1);

上面两个都可以返回关联的最新一条但是结构不同

A[xxxx]

B[[xxxx]]

发表在 None | 留下评论

php curl

1.先初始化 curl_init() 变量

2.设置变量的 URL header 超时 DNS curl_setopt()

3.执行 curl_exec()

4.检查执行结果

5.关闭curl curl_close()

    $ch = curl_init();
    
    // 设置 CURL 核心参数
    curl_setopt($ch, CURLOPT_URL, $url);
    // 将结果返回而不是直接输出
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // 模拟浏览器 User-Agent(和原代码一致)
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36');
    
    // ========== 针对你纽约VPS的关键优化 ==========
    // 连接超时(重点!比原代码的10秒短,避免卡死)
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
    // 总请求超时(建议3秒内,比原10秒更合理)
    curl_setopt($ch, CURLOPT_TIMEOUT, 3);
    // 启用DNS缓存,避免重复解析域名拖慢速度
    curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, true);
    // 关闭TCP延迟,提升传输速度
    curl_setopt($ch, CURLOPT_TCP_NODELAY, true);
    // 禁用SSL验证(因为你用的是http,这行只是兜底,防止后续改https出问题)
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $response = curl_exec($ch);
    if ($response === false) {
            $errorMsg = curl_error($ch);
            $errorCode = curl_errno($ch);
            throw new Exception("CURL请求失败 [{$errorCode}]:{$errorMsg}");
        }
            // 保留原有的编码转换逻辑(GBK转UTF-8)
        $utf8Str = mb_convert_encoding($response, 'UTF-8', 'GBK');
        
        // 关闭CURL资源
        curl_close($ch);

发表在 None | 留下评论