slim / slim
Slim 是一个 PHP 微型框架,帮助您快速编写简单而强大的 Web 应用程序和 API
Requires
- php: ^7.4 || ^8.0
- ext-json: *
- nikic/fast-route: ^1.3
- psr/container: ^1.0 || ^2.0
- psr/http-factory: ^1.1
- psr/http-message: ^1.1 || ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- psr/log: ^1.1 || ^2.0 || ^3.0
Requires (Dev)
- ext-simplexml: *
- adriansuter/php-autoload-override: ^1.4
- guzzlehttp/psr7: ^2.6
- httpsoft/http-message: ^1.1
- httpsoft/http-server-request: ^1.1
- laminas/laminas-diactoros: ^2.17 || ^3
- nyholm/psr7: ^1.8
- nyholm/psr7-server: ^1.1
- phpspec/prophecy: ^1.19
- phpspec/prophecy-phpunit: ^2.1
- phpstan/phpstan: ^1.11
- phpunit/phpunit: ^9.6
- slim/http: ^1.3
- slim/psr7: ^1.6
- squizlabs/php_codesniffer: ^3.10
- vimeo/psalm: ^5.24
Suggests
- ext-simplexml: Needed to support XML format in BodyParsingMiddleware
- ext-xml: Needed to support XML format in BodyParsingMiddleware
- php-di/php-di: PHP-DI is the recommended container library to be used with Slim
- slim/psr7: Slim PSR-7 implementation. See https://slim.php.ac.cn/docs/v4/start/installation.html for more information.
- 5.x-dev
- 4.x-dev
- 4.14.0
- 4.13.0
- 4.12.0
- 4.11.0
- 4.10.0
- 4.9.0
- 4.8.1
- 4.8.0
- 4.7.1
- 4.7.0
- 4.6.0
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.0
- 4.1.0
- 4.0.0
- 4.0.0-beta
- 4.0.0-alpha
- 3.x-dev
- 3.12.5
- 3.12.4
- 3.12.3
- 3.12.2
- 3.12.1
- 3.12.0
- 3.11.0
- 3.10.0
- 3.9.2
- 3.9.1
- 3.9.0
- 3.8.1
- 3.8.0
- 3.7.0
- 3.6.0
- 3.5.0
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.0
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.0
- 3.0.0
- 3.0.0-RC3
- 3.0.0-RC2
- 3.0.0-RC1
- 3.0.0-beta2
- 3.0-beta1
- 2.x-dev
- 2.6.3
- 2.6.2
- 2.6.1
- 2.6.0
- 2.5.0
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.5
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.0
- 1.6.7
- 1.6.6
- 1.6.5
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
This package is auto-updated.
Last update: 2024-09-20 17:56:18 UTC
README
Slim 是一个 PHP 微型框架,帮助您快速编写简单而强大的 Web 应用程序和 API。
安装
建议您使用 Composer 安装 Slim。
$ composer require slim/slim
这将安装 Slim 及其所有依赖项。Slim 需要 PHP 7.4 或更高版本。
选择 PSR-7 实现 & ServerRequest 创建者
在您可以使用 Slim 运行之前,您需要选择一个最适合您应用的 PSR-7 实现。以下是一些值得注意的选项
- Slim-Psr7 - 这是 Slim 框架的 PSR-7 实现
- httpsoft/http-message & httpsoft/http-server-request - 这是最快、最严格和最轻量级的实现
- Nyholm/psr7 & Nyholm/psr7-server - 性能几乎与 HttpSoft 实现相同
- Guzzle/psr7 - 这是 Guzzle 客户端使用的实现,具有额外的流和文件处理功能
- laminas-diactoros - 这是 Laminas (Zend) 的 PSR-7 实现
Slim-Http 装饰器
Slim-Http 是一套用于任何 PSR-7 实现的装饰器,我们推荐与 Slim 框架一起使用。要安装 Slim-Http 库,只需运行以下命令
composer require slim/http
ServerRequest
和 Response
对象装饰器将由内部工厂自动检测和应用。如果您已安装 Slim-Http 并希望关闭自动对象装饰,则可以使用以下语句
<?php use Slim\Factory\AppFactory; use Slim\Factory\ServerRequestCreatorFactory; AppFactory::setSlimHttpDecoratorsAutomaticDetection(false); ServerRequestCreatorFactory::setSlimHttpDecoratorsAutomaticDetection(false); $app = AppFactory::create(); // ...
Hello World 使用带有 PSR-7 自动检测的 AppFactory
为了使自动检测工作并允许您使用 AppFactory::create()
和 App::run()
而无需手动创建 ServerRequest
,您需要安装以下实现之一
- Slim-Psr7 - 使用
composer require slim/psr7
安装 - httpsoft/http-message & httpsoft/http-server-request - 使用
composer require httpsoft/http-message httpsoft/http-server-request
安装 - Nyholm/psr7 & Nyholm/psr7-server - 使用
composer require nyholm/psr7 nyholm/psr7-server
安装 - Guzzle/psr7 - 使用
composer require guzzlehttp/psr7
安装 - laminas-diactoros - 使用
composer require laminas/laminas-diactoros
安装
然后创建文件 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
访问 https://: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许可。有关更多信息,请参阅许可文件。