SlimPHP開發指南四:Slim\App

靜好先森發表於2019-04-10

教科書般的PHP框架學習指南

注:翻譯水平有限,如有錯誤,歡迎指正

概述

The Application, (or Slim\App) is the entry point to your Slim application and is used to register the routes that link to your callbacks or controllers.

應用程式(或Slim\App)是Slim應用程式的入口點,用於註冊連結到回撥或控制器的路由。

// instantiate the App object
$app = new \Slim\App();

// Add route callbacks
$app->get('/', function ($request, $response, $args) {
    return $response->withStatus(200)->write('Hello World!');
});

// Run application
$app->run();
複製程式碼

Application配置

The Application accepts just one argument. This can be either a Container instance or an array to configure the default container that is created automatically.

Application只接受一個引數。這可以是一個容器例項,也可以是一個陣列,用於配置自動建立的預設容器。

There are also a number of settings that are used by Slim. These are stored in the settings configuration key. You can also add your application-specific settings.

Slim還使用了一些設定。這些儲存在settings配置鍵中。您還可以新增特定於應用程式的設定。

For example, we can set the Slim setting displayErrorDetails to true and also configure Monolog like this:

例如,我們可以將Slim中的displayErrorDetails配置設定為true,還可以這樣配置Monolog:

$config = [
    'settings' => [
        'displayErrorDetails' => true,

        'logger' => [
            'name' => 'slim-app',
            'level' => Monolog\Logger::DEBUG,
            'path' => __DIR__ . '/../logs/app.log',
        ],
    ],
];
$app = new \Slim\App($config);
複製程式碼

查詢設定

As the settings are stored in the DI container so you can access them via the settings key in container factories. For example:

由於設定儲存在DI容器中,所以可以通過容器工廠中的設定鍵訪問它們。例如:

$loggerSettings = $container->get('settings')['logger'];
複製程式碼

You can also access them in route callables via $this:

你也可以通過$this在route callables中訪問它們:

$app->get('/', function ($request, $response, $args) {
    $loggerSettings = $this->get('settings')['logger'];
    // ...
});
複製程式碼

更新設定

If you need to add or update settings stored in the DI container after the container is initialized, you can use the replace method on the settings container. For example:

如果需要在初始化容器之後新增或更新儲存在DI(Dependency injection)容器中的設定,可以在設定容器上使用replace方法。例如:

$settings = $container->get('settings');
$settings->replace([
    'displayErrorDetails' => true,
    'determineRouteBeforeAppMiddleware' => true,
]);
複製程式碼

Slim 預設設定

Slim has the following default settings that you can override:

Slim有以下可以覆蓋的預設設定:

httpVersion

The protocol version used by the Response object. (Default: '1.1')

responseChunkSize

Size of each chunk read from the Response body when sending to the browser. (Default: 4096)

傳送到瀏覽器時從響應體讀取的每個塊的大小。(預設:4096)

outputBuffering

If false, then no output buffering is enabled. If 'append' or 'prepend', then any echo or print statements are captured and are either appended or prepended to the Response returned from the route callable. (Default: 'append')

如果為false,則不啟用輸出緩衝。如果“append”或“prepend”,則捕獲任何echo或print語句,並將其附加到可呼叫路由返回的響應中。(預設:append)

determineRouteBeforeAppMiddleware

When true, the route is calculated before any middleware is executed. This means that you can inspect route parameters in middleware if you need to. (Default: false)

如果為真,則在執行任何中介軟體之前運算路由。這意味著如果需要,您可以在中介軟體中檢查路由引數(預設:false)

displayErrorDetails

When true, additional information about exceptions are displayed by the default error handler. (Default: false)

如果為真,則預設錯誤處理程式將顯示關於異常的附加資訊。(預設:false)

####addContentLengthHeader When true, Slim will add a Content-Length header to the response. If you are using a runtime analytics tool, such as New Relic, then this should be disabled. (Default: true)

如果為真,Slim將向響應新增一個Content-Length頭部。如果您正在使用執行時分析工具,比如New Relic,那麼應該禁用該工具。(Default: true)

routerCacheFile

Filename for caching the FastRoute routes. Must be set to to a valid filename within a writeable directory. If the file does not exist, then it is created with the correct cache information on first run. Set to false to disable the FastRoute cache system. (Default: false)

用於快取FastRoute路由的檔名。必須設定為可寫目錄中的有效檔名。如果檔案不存在,則在第一次執行時使用正確的快取資訊建立它。 設定為false以禁用FastRoute快取系統。(預設:false)

相關文章