shiroi blog

This is my blog...

laravel获取路由的两种方式

$routes = app()->router->getRoutes();
//第一种
foreach ($routes as $k => $value) {
    if($value->action['namespace'] == 'App\Http\Controllers\Api') {
        $path[$k]['uri'] = $value->uri;
        $path[$k]['path'] = $value->methods[0];
    }
}
dump($path);

//第二种
$data = [];
$routes = collect($routes)->map(function ($route) use ($data) {
    if($route->action['namespace'] == 'App\Http\Controllers\Api') {
        $data['uri'] = $route->uri;
        $data['path'] = $route->methods[0];
    }
    return $data;
})->filter()->all();
dump($routes);

根据分类实现无限级

数组

$array = [
    [
        "username"=>"xiaxian2",
        "headimg"=>"/Templates/home/res/face/3.png",
        "money"=>"99500.00",
        "yongjin"=>"0.00",
        "yongjingai"=>"0",
        "jiesuan"=>"0.00",
        "agent"=>"8e5d35bf1414296e03c7846ebab7af6e",
        "userid"=>"09b42ce90879719a67f050b431b03534",
        "zongliushui"=>"500",
        "zongyinkui"=>null
    ],
    [
        "username"=>"erjixiaxian2",
        "headimg"=>"/Templates/home/res/face/3.png",
        "money"=>"99400.00",
        "yongjin"=>"0.00",
        "yongjingai"=>"0",
        "jiesuan"=>"0.00",
        "agent"=>"e89c3cb72cc95e2e7014d3d07b97beb2",
        "userid"=>"2a1ab4890db2142c6c0bb51ef77994fb",
        "zongliushui"=>"600",
        "zongyinkui"=>null
    ],
    [
        "username"=>"erjixiaxian",
        "headimg"=>"/Templates/home/res/face/3.png",
        "money"=>"100592.80",
        "yongjin"=>"0.00",
        "yongjingai"=>"0",
        "jiesuan"=>"0.00",
        "agent"=>"e89c3cb72cc95e2e7014d3d07b97beb2",
        "userid"=>"6256d265dcc1e9cfca2c37c1093444cb",
        "zongliushui"=>"600",
        "zongyinkui"=>"592.8"
    ],
    [
        "username"=>"admin",
        "headimg"=>"/Templates/home/res/face/2.png",
        "money"=>"11038.83",
        "yongjin"=>"1.50",
        "yongjingai"=>"1",
        "jiesuan"=>"7.50",
        "agent"=>"7d9e993765d4f79748ec54cbed9a996e",
        "userid"=>"8e5d35bf1414296e03c7846ebab7af6e",
        "zongliushui"=>"5821",
        "zongyinkui"=>"8351.548"
    ],
    [
        "username"=>"xiaxian1",
        "headimg"=>"/Templates/home/res/face/3.png",
        "money"=>"97494.00",
        "yongjin"=>"0.00",
        "yongjingai"=>"0",
        "jiesuan"=>"0.00",
        "agent"=>"8e5d35bf1414296e03c7846ebab7af6e",
        "userid"=>"e89c3cb72cc95e2e7014d3d07b97beb2",
        "zongliushui"=>"3500",
        "zongyinkui"=>"-2006"
    ]
];

引用算法

//引用算法
function generateTree($array){
    //第一步 构造数据
    $items = array();
    foreach($array as $value){
        $items[$value['userid']] = $value;
    }
    //第二部 遍历数据 生成树状结构
    $tree = array();
    foreach($items as $key => $value){
        if(isset($items[$value['agent']])){
            $items[$value['agent']]['son'][] = &$items[$key];
        }else{
            $tree[] = &$items[$key];
        }
    }
    return $tree;
}

$a = generateTree($array);
dd($a);

结果

[
    {
        "username": "admin",
        "headimg": "/Templates/home/res/face/2.png",
        "money": "11038.83",
        "yongjin": "1.50",
        "yongjingai": "1",
        "jiesuan": "7.50",
        "agent": "7d9e993765d4f79748ec54cbed9a996e",
        "userid": "8e5d35bf1414296e03c7846ebab7af6e",
        "zongliushui": "5821",
        "zongyinkui": "8351.548",
        "son": [
            {
                "username": "xiaxian2",
                "headimg": "/Templates/home/res/face/3.png",
                "money": "99500.00",
                "yongjin": "0.00",
                "yongjingai": "0",
                "jiesuan": "0.00",
                "agent": "8e5d35bf1414296e03c7846ebab7af6e",
                "userid": "09b42ce90879719a67f050b431b03534",
                "zongliushui": "500",
                "zongyinkui": null
            },
            {
                "username": "xiaxian1",
                "headimg": "/Templates/home/res/face/3.png",
                "money": "97494.00",
                "yongjin": "0.00",
                "yongjingai": "0",
                "jiesuan": "0.00",
                "agent": "8e5d35bf1414296e03c7846ebab7af6e",
                "userid": "e89c3cb72cc95e2e7014d3d07b97beb2",
                "zongliushui": "3500",
                "zongyinkui": "-2006",
                "son": [
                    Array(),
                    Array()
                ]
            }
        ]
    }
]

阅读

mysql 数据库关联多张表

表一:

CREATE TABLE `log_zz` (
  `dt` datetime NOT NULL,
  `info` varchar(100) NOT NULL,
  KEY `dt` (`dt`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

表二:

CREATE TABLE `log_xx` (
  `dt` datetime NOT NULL,
  `info` varchar(100) NOT NULL,
  KEY `dt` (`dt`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

表三:

CREATE TABLE `log_yy` (
  `dt` datetime NOT NULL,
  `info` varchar(100) NOT NULL,
  KEY `dt` (`dt`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

关联表:

CREATE TABLE `log_merge` (
  `dt` datetime NOT NULL,
  `info` varchar(100) NOT NULL,
  KEY `dt` (`dt`)
) ENGINE=MRG_MyISAM DEFAULT CHARSET=utf8 UNION=(`log_zz`,`log_xx`,`log_yy`);

这是三张表的数据:
D5Q7}IL{10TRQGAF@47D~61.png

这是关联表的数据:

关联表数据

执行代码

查询:select * from log_merge;
删除:delete from log_merge where `dt`='2020-08-10 10:48:53';
更新:update table where  `dt`='2020-08-10 10:48:53' set `info` = 'SS';

composer的自动加载机制

composer支持四种模式(psr0,psr4,classmap,files)

composer默认psr4,支持php5.2
PSR-4指定的就当作当前命名空间的目录, 而PSR-0 指定的是当前命名空间的父目录。composer dump-autoload 一下

<?php
//实现自动加载类
spl_autoload_register(function ($class) {
    /* 限定类名路径映射 */
    $class_map = array(
        // 限定类名 => 文件路径
        $class => $class.".php",
    );
    /* 引入相关文件 */
    if (file_exists($file = $class_map[$class])) include $file;
});
//按照顺序加载(按照命名空间寻找)
$aa =new \classes\AA();
$aa->aa();
echo "\n";
$bb = new \classes\BB();
$bb->bb();
echo "\n";
$sasasaasds = new \saadsadsas\sasasaasds();
$sasasaasds->wqwqwq();
echo "\n";
$fwqdqwd = new \wqeqweqw\fwqdqwd();
$fwqdqwd->尼玛();

sql_autoload_register() 函数 跟 __autoload()相比,
1.__autoload($class) 因为是一个函数,所以只能定义一次,使用多个会冲突报错;而 sql_autoload_register('function') 可定义多个,它有效地创建一个队列的自动装载函数并按顺序依次定义

2.SPL函数很丰富,有更多的操作空间:如spl_autoload_unregister()注销已经注册的函数、spl_autoload_functions()返回所有已经注册的函数等

mac搭建自己的微服务

这里使用的是laravel框架,基于laravel的组件larvels(laravel+swoole)

LaravelS 基于 Swoole 加速 Laravel/Lumen,常驻内存,内置 HTTP/WebSocket Server,支持 TCP/UDP Server、自定义进程、异步的事件监听、异步任务队列、毫秒级定时任务、平滑 Reload,与 Nginx 配合搭建高可用分布式服务器群,开箱即用。

1、 安装laravel框架(这步直接省略)
2、 直接安装laravelS

composer require "hhxsv5/laravel-s:~3.5.0" -vvv

3、 注册Service Provider
修改文件 config/app.php

 'providers' => [
        Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class,
    ],

4、 发布配置和二进制文件

php artisan laravels publishconfig\laravels.php就是你配置文件

5、 运行

php bin/laravels start

1F999F85-406B-4AD7-8E72-B95CD21F632E.png
这样就算是初步完成启动了,通过config\laravels.php配置的端口通过http请求完成访问

6.将ip发布到nginx并配置一个域名

gzip on;
gzip_min_length 1024;
gzip_comp_level 2;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml application/x-httpd-php image/jpeg image/gif image/png font/ttf font/otf image/svg+xml;
gzip_vary on;
gzip_disable "msie6";
#nginx upstream用于负载均衡
upstream swoole {
    # 通过 IP:Port 连接
    #多服务器负载均衡(没有服务器,拿端口来顶替),模拟多个服务器
    server 127.0.0.1:5200 weight=5 max_fails=3 fail_timeout=5s;#可设置参数请求
    server 127.0.0.1:5201 weight=5 max_fails=3 fail_timeout=5s;#可设置参数请求
    # 通过 UnixSocket Stream 连接,小诀窍:将socket文件放在/dev/shm目录下,可获得更好的性能
    #server unix:/yourpath/laravel-s-test/storage/laravels.sock weight=5 max_fails=3 fail_timeout=30s;
    #server 192.168.1.1:5200 weight=3 max_fails=3 fail_timeout=30s;
    #server 192.168.1.2:5200 backup;
    keepalive 16;
}

server {
    listen       80;
    client_max_body_size 512m;
    server_name  aa.com;
    # root  /Users/huchaoran/Desktop/所有项目/muchTemplate/public;
    autoindex off;
    index index.html index.php;

    location / {
        # try_files $uri $uri/ /index.php?$query_string;
        try_files $uri @laravels;
    }
    # location ~ \.php(.*)$ {
    #       fastcgi_pass   127.0.0.1:9000;
    #        fastcgi_index  index.php;
    #        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
    #        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    #        fastcgi_param  PATH_INFO  $fastcgi_path_info;
    #        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
    #       include        fastcgi_params;
    # }
    # location ~ /\.ht {
    #     deny all;
    # }
    # if (!-e $request_filename) {
    #     rewrite ^/(.*)$ /index.php/$1;
    # }
    location @laravels {
        # proxy_connect_timeout 60s;
        # proxy_send_timeout 60s;
        # proxy_read_timeout 120s;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-PORT $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header Server-Protocol $server_protocol;
        proxy_set_header Server-Name $server_name;
        proxy_set_header Server-Addr $server_addr;
        proxy_set_header Server-Port $server_port;
        access_log  /Users/huchaoran/Desktop/所有项目/muchTemplate/access.log;
        # “swoole”是指上面的upstream定义的swoole
        proxy_pass http://swoole;
    }
}

这里就完成了搭建laravel微服务,后面的我会一步步更新的

备案号:粤ICP备18155514号-2