# 歡迎
Slim是一個PHP微框架,可以幫助您快速編寫簡單但功能強大的web應用程序和api。在其核心,Slim是一個dispatcher,它接收HTTP請求,調用適當的回調例程,并返回HTTP響應。就是這樣。
# 重點是什么?
Slim是創建使用、重用或發布數據的api的理想工具。Slim也是快速原型制作的一個很好的工具。甚至可以用用戶界面構建功能完整的web應用程序。更重要的是,Slim速度非常快,并且只有很少的代碼。事實上,你可以在一個下午閱讀和理解它的源代碼!
> 在其核心,Slim是一個dispatcher,它接收HTTP請求,調用適當的回調例程,并返回HTTP響應。就是這樣。
你并不總是需要像Symfony或Laravel那樣的全方位方案。毫無疑問,這些都是很棒的工具。但它們往往是多余的。相反,Slim只提供了很少的一組工具來完成您需要的工作,其他的什么也不提供。
# 它是如何工作的?
首先,您需要像Nginx或Apache這樣的web服務器。您應該配置您的web服務器,以便它將所有適當的請求發送到一個“前端控制器”PHP文件。在這個PHP文件中實例化并運行Slim應用程序。
一個slim應用程序包含響應特定HTTP請求的路由。每個路由調用一個回調并返回一個HTTP響應。首先要實例化和配置Slim應用程序。接下來,定義應用程序路由。最后,運行slim應用程序。它是那么容易。下面是一個應用程序示例:
```
<?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
*
* In order for the factory to work you need to ensure you have installed
* a supported PSR-7 implementation of your choice e.g.: Slim PSR-7 and a supported
* ServerRequest creator (included with Slim PSR-7)
*/
$app = AppFactory::create();
// Add Routing Middleware
$app->addRoutingMiddleware();
/*
* Add Error Handling Middleware
*
* @param bool $displayErrorDetails -> Should be set to false in production
* @param bool $logErrors -> Parameter is passed to the default ErrorHandler
* @param bool $logErrorDetails -> Display error details in error log
* which can be replaced by a callable of your choice.
* Note: This middleware should be added last. It will not handle any exceptions/errors
* for middleware added after it.
*/
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
// Define app routes
$app->get('/hello/{name}', function (Request $request, Response $response, $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});
// Run app
$app->run();
```
圖1:Slim應用程序示例
# 請求和響應
When you build a Slim app, you are often working directly with Request and Response objects. These objects represent the actual HTTP request received by the web server and the eventual HTTP response returned to the client.
當您構建一個slim應用程序時,您經常直接處理請求和響應對象。這些對象表示web服務器接收到的實際HTTP請求和返回給客戶機的最終HTTP響應。
Every Slim app route is given the current Request and Response objects as arguments to its callback routine. These objects implement the popular[PSR-7](http://www.slimframework.com/docs/v4/concepts/value-objects.html)interfaces. The Slim app route can inspect or manipulate these objects as necessary. Ultimately, each Slim app route**MUST**return a PSR-7 Response object.
每個slim應用程序路由都將當前請求和響應對象作為其回調例程的參數。這些對象實現了流行的PSR-7接口。Slim應用程序路由可以根據需要檢查或操作這些對象。最后,每個slim應用程序路由必須返回一個PSR-7響應對象。
# 自帶組件
Slim is designed to play well with other PHP components, too. You can register additional first-party components such as[Slim-Csrf](https://github.com/slimphp/Slim-Csrf/),[Slim-HttpCache](https://github.com/slimphp/Slim-HttpCache), or[Slim-Flash](https://github.com/slimphp/Slim-Flash)that build upon Slim’s default functionality. It’s also easy to integrate third-party components found on[Packagist](https://packagist.org/).
Slim也可以很好地與其他PHP組件配合使用。您可以注冊附加的第一方組件,如Slim- csrf、Slim- httpcache或Slim- flash,這些組件建立在Slim的默認功能之上。集成Packagist上的第三方組件也很容易。
# 如何閱讀本文檔
如果您是Slim的新手,我建議您從頭到尾閱讀本文。如果您已經熟悉Slim,則可以直接跳到相應的部分。
本文檔首先解釋了Slim的概念和體系結構,然后討論了請求和響應處理、路由和錯誤處理等特定主題。
- 開始
- 安裝
- 升級指南
- Web服務器
- 概念
- 生命周期
- PSR 7
- 中間件
- 依賴容器
- 實例 及通知和警告處理
- Request
- 請求方法
- 請求頭信息
- 請求主體
- 上傳的文件
- 請求幫助
- 路由對象
- Response
- 響應狀態
- 響應標頭
- 響應體
- 返回JSON
- 視圖模板
- 路由
- 創建路由
- 路由回調
- 路由策略
- 路線占位符
- 路由名
- 路由組
- 路由中間件
- 路由表達式緩存
- 容器識別解析
- 封裝中間件
- 路由的中間件
- 錯誤處理中間件
- 方法重寫的中間件
- 輸出緩沖中間件
- 內容長度中間件
- 擴展功能
- 以 / 結尾的路由模式
- 獲取當前路由
- 設置CORS
- 使用POST表單上傳文件
- 第三方組件
- slim-session
- auth
- slim-api-skeleton
- dir