Helvetica / 标准
标准PHP框架
1.0.1
2018-10-10 12:55 UTC
Requires
- php: >=5.6.0
- guzzlehttp/guzzle: ^6.3
- psr/container: ^1.0
Requires (Dev)
- phpunit/phpunit: ^7.4
This package is not auto-updated.
Last update: 2024-09-23 18:22:56 UTC
README
简单即强大
安装
composer require helvetica/standard 1.0.1
导入composer自动加载
require('vendor/autoload.php');
开始使用
use Helvetica\Standard\App; use Helvetica\Standard\Router; use Helvetica\Standard\Library\Response; $router = new Router(); $router->set('/hello/<name>', function(Response $response, $name) { return $response->withContent('hello ' . $name); }); (new App)->start();
使用内置服务器进行测试
php -S localhost:8080
访问 https://:8080/hello/world 以显示: "hello world"
使用模板
hello.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h1>hello <?= $name ?></h1> </body> </html>
index.php
use Helvetica\Standard\App; use Helvetica\Standard\Router; use Helvetica\Standard\Library\Template; use Helvetica\Standard\Library\Response; $router = new Router(); $router->set('/hello/<name>', function(Response $response, Template $temp, $name) { $output = $temp->render(__DIR__ . '/hello.php', ['name' => $name]); return $response->withContent($output); }); (new App)->start();
访问 https://:8080/hello/world 以显示: "hello world"
控制器类
index.php
use Helvetica\Standard\App; use Helvetica\Standard\Router; use Helvetica\Standard\Library\Response; class PrintController { public function hello(Response $response, $name) { return $response->withContent('hello ' . $name); } } $router = new Router(); $router->set('/hello/<name>', [PrintController::class, 'hello']); (new App)->start();
访问 https://:8080/hello/world 以显示: "hello world"
使用过滤器
use Helvetica\Standard\App; use Helvetica\Standard\Router; use Helvetica\Standard\Library\Request; use Helvetica\Standard\Library\Response; use Helvetica\Standard\Abstracts\ActionFilter; class SayHelloFilter extends ActionFilter { /** * The method hook is injectable, and has a fixed param $next. * You can call and return the $next closure to continue * or just return a response to terminate the process. */ public function hook(Request $request, $next) { $params = $this->getParams(); $name = $params['name']; $request->withAttributes(['text' => 'hello ' . $name]); return $next(); } } $router = new Router(); $router->set('/hello/<name>', function(Request $request, Response $response, $name) { $text = $request->getAttribute('text'); return $response->withContent($text); })->setFilters([SayHelloFilter::class]); (new App)->start();
访问 https://:8080/hello/world 以显示: "hello world"
使用处理器
让我们抛出一个404未找到异常,并注册一个未找到处理器。
use Helvetica\Standard\App; use Helvetica\Standard\Router; use Helvetica\Standard\Exception\NotFoundException; use Helvetica\Standard\Abstracts\HttpExceptionHandler; $router = new Router(); $router->set('/not-found', function() { throw new NotFoundException(); }); // create a not found handler class MyNotFoundHandler extends HttpExceptionHandler { public function getResponse(Response $response) { return $response->withContent('This is my not found exception message.'); } } $app = new App(); $app->setHandler(App::HANDLE_NOT_FOUND, MyNotFoundHandler::class); $app->start();
访问 https://:8080/not-found 以显示: "这是我的未找到异常消息"。