分类目录归档:PHP

Twig 快速集成

假设你的项目项目使用composer

1. 安装,为了兼容php5.4使用的是1.x版本

composer require twig/twig:~1.0

2. 封装为trait以集成

namespace objects\Traits;

trait DoufuTwig {

    /**
     * @var \Twig_Environment
     */
    private $twig;

    protected function renderTwig($path, $data = []) {
        if (! $this->twig) {
            $loader = new \Twig_Loader_Filesystem(APPPATH . 'views');
            $this->twig = new \Twig_Environment($loader, 
                array(
                    'cache' => APPPATH . 'cache',
                    'auto_reload' => true
                ));
        }
        
        return $this->twig->render($path, $data);
    }
}

3. 使用

集成到控制器父类

namespace Ncontrollers;

use objects\Traits\DoufuTwig;

abstract class Controller
{
    use DoufuTwig;
}

控制器内使用

...
echo $this->renderTwig('xxx/login.html');
...

奇异的setcookie无效问题

api.xxxx.com/aaa 页面下setcookie,

api.xxxx.com/xxx,cookie 有效

api.xxxx.com/index.php/xxx,cookie 无效

其实是cookie path的问题

setcookie($key, $value, null, ‘/’, null, null);

在框架外套一个路由器,单独处理部分请求

...前略

$_run_ci_ = false;

$_methods_ = 'GET|POST|PUT|DELETE|OPTIONS|PATCH|HEAD';

if (isset($_SERVER['REQUEST_METHOD']) &&
     in_array(strtoupper($_SERVER['REQUEST_METHOD']), explode('|', $_methods_))) {
    // Create Router instance
    $router = new \Bramus\Router\Router();
    
    # loader
    include APPPATH . 'hooks/doufu_autoload.php';
    $_doufu_loader_ = new doufu_autoload();
    $_doufu_loader_->run();
    
    // Define routes
    require_once dirname(__FILE__) . "/../../app/config/router.php";
    # default router
    $router->match($_methods_, '.*', function () use (&$_run_ci_) {
        $_run_ci_ = true;
    });
    
    // Run it!
    $router->run();
} else {
    $_run_ci_ = true;
}

if ($_run_ci_) {
    require_once BASEPATH . 'core/CodeIgniter.php';
}

路由器项目地址:https://github.com/bramus/router

用php监控从库同步延迟个数

<?php
$mysqli = new mysqli('slave_host', 'root', 'passwd', 'dbname');

/*
 * This is the "official" OO way to do it,
 * BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
 */
if ($mysqli->connect_error) {
 die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}

/*
 * Use this instead of $connect_error if you need to ensure
 * compatibility with PHP versions prior to 5.2.9 and 5.3.0.
 */
if (mysqli_connect_error()) {
 die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}

echo 'Success... ' . $mysqli->host_info . "\n";

/* Create table doesn't return a resultset */
/**
 * 
 * @var mysqli_result $result
 */
$result = $mysqli->query("show slave status");

if ($mysqli->errno) {
 printf("Error number: %s\n", $mysqli->errno);
 printf("Error message: %s\n", $mysqli->error);
}

if ($result !== false) {
 $row = $result->fetch_assoc();
 echo "master : {$row['Master_Log_File']}\n";
 echo "slave relay : {$row['Relay_Master_Log_File']}\n";
 echo " : " . ($row['Read_Master_Log_Pos'] - $row['Exec_Master_Log_Pos']);
}

$mysqli->close();

php pecl命令使用代理上网

[root@web4 ~]# pecl config-set http_proxy http://test:1080
config-set (http_proxy, http://test:1080, user) failed, channel pecl.php.net

尝试失败

查找资料https://blog.flowl.info/2015/peclpear-behind-proxy-how-to/

换成pear成功。。。。

[root@web4 ~]# pear config-set http_proxy http://test:1080
config-set succeeded

这个应该是个bug pecl的代理要使用pear来设置。。。