依赖容器(Dependency Container)

    你并不必须提供一个依赖容器。如果你提供了,那么,你必须注入此容器的实例到 Slim 应用程序的构造函数中。

    你可以显式或隐式地从依赖容器中获取服务。你可以像下面这样子从 Slim 应用程序的路由中获取一个显示的容器实例。

    你可以这样隐式地从容器中取得服务:

    Slim uses and isset() magic methods that defer to the application’s container for all properties that do not already exist on the application instance.

    必需的服务

    你的容器必须实现这些必需的服务。如果你使用的是 Slim 内置的容器,这些服务都是已经准备好了的。如果你选择使用第三方容器,那么你必须自己来实现这些服务。

    settings

    • httpVersion
    • responseChunkSize
    • outputBuffering
    • displayErrorDetails.environment

    \Slim\Interfaces\Http\EnvironmentInterface 的实例.

    request

    的实例.

    response

    \Psr\Http\Message\ResponseInterface的实例.

    router

    foundHandler

    \Slim\Interfaces\InvocationStrategyInterface 的实例.

    phpErrorHandler

    PHP 7 错误被抛出时调用的 Callable。这个 callable 必须返回一个 \Psr\Http\Message\ResponseInterface 的实例,并接收三个参数:

    • \Psr\Http\Message\ServerRequestInterface
    • \Psr\Http\Message\ResponseInterface
    • \ErrorerrorHandler

    抛出异常时调用的 Callable。这个 callable 必须返回一个 \Psr\Http\Message\ResponseInterface 的实例,并接收三个参数:

    • \ExceptionnotFoundHandler

    如果当前的 HTTP 请求 URI 未能匹配到应用程序路由,则调用这个 Callable。这个 callable 必须返回一个 \Psr\Http\Message\ResponseInterface 的实例,并接收三个参数:

    • \Psr\Http\Message\ServerRequestInterface
    • \Psr\Http\Message\ResponseInterfacenotAllowedHandler
    • \Psr\Http\Message\ServerRequestInterface
    • \Psr\Http\Message\ResponseInterface

    的实例.