borsch/framework

一个PSR-15 API框架,助您快速启动项目。

v1.0 2019-03-07 09:44 UTC

This package is auto-updated.

Last update: 2024-09-07 21:51:19 UTC


README

Borsch是一个微框架,可以帮助您高效地启动应用程序。

上下文

让我们面对现实,现在是2019年,PHP已经不再适合构建完整的网站。
模板引擎已经过时,现在已被JavaScript框架所取代,更快、更强大。

然而,PHP仍然非常适合创建服务器端服务,例如API。
PSR在这方面有很大帮助,每个组件都可以相互兼容!

在此背景下,Borsch是与最佳包一起创建的。
您所喜爱的、且熟悉如何使用的一切,都汇集在一个小巧的包中,以帮助您实现下一个想法。

安装

推荐使用 Composer

composer require borsch/framework

使用方法

Borsch使用 thephpleague/route 包来处理路由,因此它看起来与 Slim 非常相似。

基本示例

require_once __DIR__.'/vendor/autoload.php';

use Borsch\{App, Factory};
use Psr\Http\Message\RequestInterface;

$app = new App();

$app->get('/', function (RequestInterface $request) {
    $response = Factory::createResponse(200, 'OK');
    $response->getBody()->write('Hello World !');

    return $response;
});

try {
    $app->run();
} catch (Exception $exception) {
    header('HTTP/1.1 500 Internal Server Error', true, 500);
    // Do something with the catched Exception
    // Log ? Email webmaster ? ...
}

使用JsonStrategy的POST示例

有关策略的更多信息,请参阅 thephpleague/route文档

 require_once __DIR__.'/vendor/autoload.php';

 use Borsch\{App, Factory};
 use League\Route\Strategy\JsonStrategy;
 use Psr\Http\Message\RequestInterface;

 $app = new App();

 $app->post('/users', function (RequestInterface $request) {
     $data = json_decode($request->getBody()->getContents(), true);
    
     return [
         'user' => [
             'firstname' => $data['user']['firstname'] ?? 'John',
             'lastname' => $data['user']['lastname'] ?? 'Doe',
             'created_at' => date('c'),
             'updated_at' => date('c')
         ]
     ];
})->setStrategy(new JsonStrategy(Factory::getFactory()));

try {
 $app->run();
} catch (Exception $exception) {
 header('HTTP/1.1 500 Internal Server Error', true, 500);
 // Do something with the catched Exception
 // Log ? Email webmaster ? ...
}

使用的包

  • league/route
  • nyholm/psr7
  • nyholm/psr7-server