Slim

Slim是一个PHP微框架,可帮助您快速编写简单但强大的Web应用程序和API。 (Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.)

Github星跟蹤圖

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

Slim 特性:

HTTP路由器
  • Slim提供了一个快速和强大的路由器,将路由回调映射到特定的HTTP请求方法和URI。 它支持参数和模式匹配。
中间件
  • 使用同心中间件构建您的应用程序,以调整Slim应用程序周围的HTTP请求和响应对象。
PSR-7支持
  • Slim支持任何PSR-7 HTTP消息实现,以便您可以检查和操纵HTTP消息方法,状态,URI,标题,Cookie和正文。

依赖注入

  • Slim支持依赖注入,因此您可以完全控制外部工具。 使用任何Container-Interop容器。

安装

建议您使用Composer来安装Slim。

$ composer require slim/slim "^3.0"

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

用法

<?php
require 'vendor/autoload.php';
$app = new Slim\App();
$app->get('/hello/{name}', function ($request, $response, $args) {
    return $response->getBody()->write("Hello, " . $args['name']);
});
$app->run();

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

$ php -S localhost:8000

转到http://localhost:8000/hello/world 现在将显示“Hello,world”。 有关如何配置Web服务器的详细信息,请参阅 文档

测试

要执行测试套件,您需要phpunit。

$ phpunit

概覽

名稱與所有者slimphp/Slim
主編程語言PHP
編程語言PHP (語言數: 1)
平台Linux, Mac, Unix-like, Windows
許可證MIT License
發布數108
最新版本名稱4.13.0 (發布於 2024-03-03 14:31:58)
第一版名稱1.0.0 (發布於 2010-11-02 08:06:07)
創建於2010-09-21 01:17:06
推送於2024-05-02 13:42:58
最后一次提交
星數11.8k
關注者數504
派生數1.9k
提交數4.5k
已啟用問題?
問題數1664
打開的問題數12
拉請求數989
打開的拉請求數1
關閉的拉請求數632
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?

Slim Framework

Build Status
Coverage Status
Total Downloads
License

Slim is a PHP micro-framework that helps you quickly write simple yet powerful web applications and APIs.

Installation

It's recommended that you use Composer to install Slim.

$ composer require slim/slim:^4.0

This will install Slim and all required dependencies. Slim requires PHP 7.1 or newer.

Choose a PSR-7 Implementation & ServerRequest Creator

Before you can get up and running with Slim you will need to choose a PSR-7 implementation that best fits your application. A few notable ones:

Slim-Http Decorators

Slim-Http is a set of decorators for any PSR-7 implementation that we recommend is used with Slim Framework.
To install the Slim-Http library simply run the following command:

composer require slim/http

The ServerRequest and Response object decorators are automatically detected and applied by the internal factories. If you have installed Slim-Http and wish to turn off automatic object decoration then you can use the following statements:

<?php

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

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

$app = AppFactory::create();

// ...

Hello World using AppFactory with PSR-7 auto-detection

In order for auto-detection to work and enable you to use AppFactory::create() and App::run() without having to manually create a ServerRequest you need to install one of the following implementations:

Then create file 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();

You may quickly test this using the built-in PHP server:

$ php -S localhost:8000 -t public

Going to http://localhost:8000/hello/world will now display "Hello, world".

For more information on how to configure your web server, see the Documentation.

Tests

To execute the test suite, you'll need to install all development dependencies.

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

Contributing

Please see CONTRIBUTING for details.

Learn More

Learn more at these links:

Security

If you discover security related issues, please email security@slimframework.com instead of using the issue tracker.

Professional support

Slim is part of Tidelift which gives software development teams a single source for purchasing and maintaining their software, with professional grade assurances from the experts who know it best, while seamlessly integrating with existing tools.

Contributors

Code Contributors

This project exists thanks to all the people who contribute. Contribute.


Financial Contributors

Become a financial contributor and help us sustain our community. Contribute

Individuals

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. Contribute










License

The Slim Framework is licensed under the MIT license. See License File for more information.

去到頂部