Grace, fastest, flexibility, simple PHP extension framework!优雅、极速、灵活、简单的PHP扩展框架!
Grace, fastest, flexibility, simple PHP extension framework!优雅、极速、灵活、简单的PHP扩展框架!
欢迎来到 Gene 框架, 一种崭新的速度最快PHP扩展框架。开源、全功能栈、使用 C 编写、极致优化的高性能框架。身处在框架社区的繁荣之下,我们都在有意或无意地追逐,简单而不简单。极简而具有扩展性的架构设计、围绕常驻内存的方式而开发,提供了命令行、容器注入、钩子、路由、缓存等开发所需的众多开箱即用的组件。一份代码同时支持PHP-FPM模式、swoole常驻模式的唯一扩展框架,可以开发高性能的web应用或者常驻内存应用,让开发者不再为框架烦恼,不再为性能忧愁,写最少的代码,做最靓的仔!
最新版本:4.2.0
官方网站:Gene框架 http://1xm.net/
框架核心特性:
加载配置文件并启动:
<?php
$app = \Gene\Application::getInstance();
$app
->load("router.ini.php")
->load("config.ini.php")
->run();
配置REST路由:支持指定类方法或者回调闭包函数;
<?php
$router = new \Gene\Router();
$router->clear()
//定义get
->get("/", "\Controllers\Index@run")
->get("/test", "\Controllers\Index@test", "@clearAll")
//定义post
->post("/",function(){
echo "index post";
})
//分组模式
->group("/admin")
->get("/:name/",function($params){
var_dump($params);
})
->get("/blog/:ext",function($params){
var_dump($params);
},"auth@clearAll")
->group()
//定义静态页面
->get("/index.html",function(){
echo 'index';
}, "@clearAfter")
//定义404
->error(404,function(){
echo " 404 ";
})
//定义自定义钩子
->hook("auth",function(){
echo " auth hook ";
return true; // 返回false中断请求
})
//全局前置钩子
->hook("before", function(){
echo " before hook ";
})
//全局后置钩子
->hook("after", function($params){
echo " after hook ";
});
配置应用变量或者对象;
<?php
$config = new \Gene\Config();
$config->clear();
//视图类注入配置
$config->set("view", [
'class' => '\Gene\View'
]);
//http请求类注入配置
$config->set("request", [
'class' => '\Gene\Request'
]);
//http响应类注入配置
$config->set("response", [
'class' => '\Gene\Response'
]);
//数据库类注入配置
$config->set("db", [
'class' => '\Gene\Db\Mysql',
'params' => [[
'dsn' => 'mysql:dbname=gene_web;host=127.0.0.1;port=3306;charset=utf8',
'username' => 'root',
'password' => '',
'options' => [PDO::ATTR_PERSISTENT => true]
]],
'instance' => true
]);
//缓存类注入配置
$config->set("memcache", [
'class' => '\Gene\Cache\Memcached',
'params' => [[
'servers' => [['host' => '127.0.0.1', 'port' => 11211]],
'persistent' => true,
]],
'instance' => true
]);
namespace Controllers;
class Index extends \Gene\Controller
{
/**
* run
*/
public function run()
{
echo 'hello world!';
}
/**
* test
*/
public function test()
{
// 模板赋值:配置文件里面配置view,调用$this->view自动注入到当前类空间
$this->view->title = "文档";
// 调用父子模板
$this->view->display('index', 'common');
}
}
<?php
define('APP_ROOT', __dir__ . '/app/');
$path = '';
if (isset($_SERVER['argv'][1])) {
$path = $_SERVER['argv'][1];
} else {
exit('This script is run as CLI');
}
$app = new Gene\Application();
$app
->autoload(APP_ROOT)
->load("router.ini.php")
->load("config.ini.php")
->run('get', $path);
框架支持常驻进程,经过完整的内存泄露测试,完美适配swoole4.X版本;
<?php
define('APP_ROOT', dirname(__dir__) . '/app');
$http = new swoole_http_server("127.0.0.1", 9501, SWOOLE_PROCESS);
//配置
$http->set([
'worker_num' => 1,
'max_request' => 10000,
'dispatch_mode'=> 2,
]);
$http->on("start", function ($server) {
echo "Swoole http server is started";
});
$http->on("request", function ($request, $response) {
$type = $request->server['request_method'] ?? "get";
$url = $request->server['request_uri'] ?? "/";
\Gene\Request::init($request->get, $request->post, $request->cookie, $request->server, null, $request->files);
\Gene\Di::set("response", $response);
ob_start();
$app = \Gene\Application::getInstance();
$app
->autoload(APP_ROOT)
->load("router.ini.php")
->load("config.ini.php")
->run($type, $url);
$out = ob_get_contents();
ob_end_clean();
$out && $response->end($out);
});
$http->start();
上述测试中,原生php文件test.php的内容:
<?php
echo 'test';
而Gene应用包含了入口文件、路由文件、配置文件、控制器等至少4个文件,但测试数据只是略低,所以确定Gene框架没有产生格外的性能损失,这是不可思议的。证明gene框架是运行速度最快的框架:Nginx+PHP-FPM模式、swoole模式下均是最快,没有之一;欢迎下载demo测试:http://1xm.net/static/test/demo.zip
phpize
./configure --enable-gene=shared
make
make install
一:湖北省教育用户认证中心(全省几百万学生、教育用户的登录入口) :http://open.e21.cn/
二:尚动电子商务平台
三:生材网 https://www.materialw.com/
php5的版本 :https://github.com/sasou/php-gene (最新版本:2.1.0,还在使用php5的朋友使用)
windows版本:https://github.com/sasou/php-gene-for-windows (已编译x86平台下的NTS版本)