教科書般的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應用程序的入口點,用於註冊連接到回調或控制器的路由。bash
// 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();
複製代碼
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.app
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.ide
Slim還使用了一些設置。這些存儲在settings配置鍵中。您還能夠添加特定於應用程序的設置。工具
For example, we can set the Slim setting displayErrorDetails to true and also configure Monolog like this:學習
例如,咱們能夠將Slim中的displayErrorDetails配置設置爲true,還能夠這樣配置Monolog:ui
$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 has the following default settings that you can override:
Slim有如下能夠覆蓋的默認設置:
The protocol version used by the Response object. (Default: '1.1')
Size of each chunk read from the Response body when sending to the browser. (Default: 4096)
發送到瀏覽器時從響應體讀取的每一個塊的大小。(默認:4096)
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)
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)
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)
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)