phpgram/phpgram

一款非常快速且轻量级的Php框架,适用于小型到企业级应用

1.8.3 2021-03-16 13:31 UTC

README

phpgram

pipeline status coverage report Packagist Version PHP from Packagist Packagist

一款非常快速且轻量级的Php框架,适用于小型到企业级应用。

  • 函数和类的路由处理器

  • 通过 Psr-7 进行请求和响应

  • 通过 Psr-15 进行中间件处理

  • 支持 Psr 11 容器,用于自动依赖注入(自动装配)路由调用:类(在构造函数中)或函数(使用 `__get()`)

  • 响应创建(通过 Psr-17 响应工厂)和策略

  • 支持异步请求

安装

通过 Composer

composer require phpgram/phpgram

文档

<?php

use Gram\App\App;

use Gram\Strategy\StdAppStrategy;

App::app()->debugMode(true);	//true (default) -> show Exceptions, false -> show empty page

App::app()->setStrategy(new StdAppStrategy()); //define how the Output will be created (StdAppStrategy is default)

App::app()->set404("Er404@404"); //404 Handler
App::app()->set405("Er405@405"); //405 Handler

//Middleware will be executed before the Router
App::app()
	->addMiddleware(new Session())
	->addMiddleware(new Authenticate());


//Routes and Groups

App::app()->get("/","Index@index"); 	//static Route

//dynamic Route with Middleware (this Middleware will only executed if the Routes is matched
App::app()->get("/video/{id:n}","Video@watch")->addMiddleware(new Caching);

//Nested Group with Middleware
// /admin will be the prefix for all Routes in this Group
App::app()->group("/admin",function (){
	
	App::app()->get("","AdminIndexHandler");
	
	App::app()->get("/role","RoleHandler");
	
	//Nested Groups
	//2. Group /settings
	App::app()->group("/settings",function (){
		//3. Group /dep
		App::app()->group("/dep",function (){
			//Routes
			App::app()->get("","DepIndexHandler");
			
			App::app()->get("/{id}/edit","EditHandler");
		});
	})->addMiddleware(new Auth());
	
})->addMiddleware(new Login());

//Group with different Output Strategy (json)
use Gram\Strategy\JsonStrategy;
App::app()->group("/",function (){
	App::app()->getpost("video/vote/{id:n}","Video@vote");
	App::app()->getpost("video/getComment/{id:n}","Video@getComments");
})->addStrategy(new JsonStrategy());


//Psr 7 Request

// in this case with Nyholm Psr 7

use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7Server\ServerRequestCreator;

$psr17Factory = new Psr17Factory();

$creator = new ServerRequestCreator($psr17Factory,$psr17Factory,$psr17Factory,$psr17Factory);
$request = $creator->fromGlobals();

//App needs a Psr 17 Response Factory
App::app()->setFactory($psr17Factory);

//get the response with the content
$response = App::app()->start($request);

//simple emit:
echo $response->getBody(); 	// don't do this in production use an psr 7 emitter!

许可证

phpgram 是开源的,遵循 MIT 许可证

致谢

路由器

  • 算法和核心实现:版权所有者 Nikita Popov。(FastRoute

  • 解析器:版权所有者 Nikita Popov 和 Phil Bennett。(thephpleague