quagga/slim

Slim 是一个 PHP 微型框架,帮助您快速编写简单而强大的 Web 应用程序和 API

4.13.0 2024-04-13 04:21 UTC

This package is auto-updated.

Last update: 2024-09-13 07:57:35 UTC


README

Build Status Coverage Status Total Downloads License

Slim 是一个 PHP 微型框架,帮助您快速编写简单而强大的 Web 应用程序和 API。

安装

建议您使用 Composer 来安装 Slim。

$ composer require slim/slim

这将安装 Slim 和所有必要的依赖项。Slim 需要 PHP 7.4 或更高版本。

选择 PSR-7 实现 & 服务器请求创建者

在您可以使用 Slim 之前,您需要选择一个最适合您应用程序的 PSR-7 实现。以下是一些值得注意的:

Slim-Http 装饰器

Slim-Http 是一组用于任何 PSR-7 实现的装饰器,我们建议与 Slim 框架一起使用。要安装 Slim-Http 库,只需运行以下命令

composer require slim/http

ServerRequestResponse 对象装饰器将由内部工厂自动检测并应用。如果您已安装 Slim-Http 并希望关闭自动对象装饰,则可以使用以下语句

<?php

use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;

AppFactory::setSlimHttpDecoratorsAutomaticDetection(false);
ServerRequestCreatorFactory::setSlimHttpDecoratorsAutomaticDetection(false);

$app = AppFactory::create();

// ...

Hello World 使用 AppFactory 和 PSR-7 自动检测

要使自动检测正常工作并允许您使用 AppFactory::create()App::run() 而无需手动创建 ServerRequest,您需要安装以下实现之一

然后创建文件 public/index.php

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

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

// Instantiate App
$app = AppFactory::create();

// Add error middleware
$app->addErrorMiddleware(true, true, true);

// Add routes
$app->get('/', function (Request $request, Response $response) {
    $response->getBody()->write('<a href="/hello/world">Try /hello/world</a>');
    return $response;
});

$app->get('/hello/{name}', function (Request $request, Response $response, $args) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");
    return $response;
});

$app->run();

您可以使用内置的PHP服务器快速测试。

$ php -S localhost:8000 -t public

现在访问 http://localhost:8000/hello/world 将显示 "Hello, world"。

有关如何配置您的Web服务器的更多信息,请参阅文档

测试

要执行测试套件,您需要安装所有开发依赖项。

$ git clone https://github.com/slimphp/Slim
$ composer install
$ composer test

贡献

有关详细信息,请参阅贡献指南

了解更多

在以下链接中了解更多信息

安全

如果您发现与安全相关的问题,请通过电子邮件 security@slimframework.com 而不是使用问题跟踪器。

企业版

作为Tidelift订阅的一部分提供。

Slim 的维护者以及数千个其他包的维护者正在与Tidelift合作,为您的应用程序构建所使用的开源依赖项提供商业支持和维护。节省时间,降低风险,并提高代码质量,同时支付您使用的确切依赖项的维护者。 了解更多。

贡献者

代码贡献者

本项目得以存在,离不开所有贡献者。 贡献

财务贡献者

成为财务贡献者,帮助我们维持社区。 贡献

个人

组织

使用您的组织支持此项目。您的标志将在这里显示,并提供到您网站的链接。 贡献

许可

Slim 框架采用MIT许可证。有关更多信息,请参阅许可文件