SlimPHP开发指南四:Slim\App

教科书般的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)

转载于:https://juejin.im/post/5cadbf99f265da03a15812a3

你可能感兴趣的:(SlimPHP开发指南四:Slim\App)