Skip to content
PHP Ecosystem
Explore

Application

API reference › PSR bridge › http › Application

classyii2\extensions\psrbridge\http\Application
Extendsyii\web\Application » yii\base\Application » yii\base\Module » yii\di\ServiceLocator » yii\base\Component » yii\base\BaseObject
ImplementsPsr\Http\Server\RequestHandlerInterface, yii\base\Configurable
Sourcesrc/http/Application.php

Handles PSR-7 requests with a stateless Yii application lifecycle.

RequestHandlerInterface PSR-7 request handler contract.

This handler requires post-emission finalization when used by a PSR-15 host. The request lifecycle spans two calls: handle() runs the request and returns the PSR-7 response (pre-send), then the runtime emits it and MUST call finalize() to complete Yii’s after-send lifecycle and release per-request worker state.

Calling handle() without finalize() leaves event tracking attached across requests in workers.

PropertyTypeDescriptionDefined by
$actionMaparray<string, class-string|array<string, mixed>>Mapping from action IDs to standalone Action class configurations. Default: []. Available since 22.0.yii\base\Module
$actionNamespacestring|nullThe namespace where standalone Action subclasses are located for convention-based discovery, parallel to $controllerNamespace for controllers. Default: null. Available since 22.0.yii\base\Module
$aliases write-onlyarrayList of path aliases to be defined.yii\base\Module
$assetManager read-onlyyii\web\AssetManagerThe asset manager application component.yii\base\Application
$authManager read-onlyyii\rbac\ManagerInterface|nullThe auth manager application component or null if it’s not configured.yii\base\Application
$basePathstringThe root directory of the application.yii\base\Application
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$bootstraparrayList of components that should be run during the application bootstrapping process. Default: [].yii\base\Application
$cache read-onlyyii\caching\CacheInterface|nullThe cache application component.yii\base\Application
$catchAllarray|nullThe configuration specifying a controller action which should handle all user requests. Default: null.yii\web\Application
$charsetstringThe charset currently used for the application. Default: 'UTF-8'.yii\base\Application
$componentsarrayThe list of the component definitions or the loaded component instances (ID => definition or instance).yii\di\ServiceLocator
$container write-onlyarrayValues given in terms of name-value pairs.yii\base\Application
$controlleryii\web\Controller|nullThe currently active controller instance Default: null.yii\web\Application
$controllerMaparrayMapping from controller ID to controller configurations. Default: [].yii\base\Module
$controllerNamespacestringThe namespace that controller classes are located in. Default: 'app\controllers'.yii\base\Application
$controllerPathstringThe directory that contains the controller classes.yii\base\Module
$db read-onlyyii\db\ConnectionThe database connection.yii\base\Application
$defaultRoutestringThe default route of this application. Default: 'site'.yii\web\Application
$errorHandler read-onlyyii\web\ErrorHandlerThe error handler application component.yii\web\Application
$extensionsarray|nullList of installed Yii extensions. Default: null.yii\base\Application
$flushLoggerboolFlushes the logger during finalize() when set to true. Default: true.Application
$formatter read-onlyyii\i18n\FormatterThe formatter application component.yii\base\Application
$homeUrlstringThe homepage URL.yii\web\Application
$i18n read-onlyyii\i18n\I18NThe internationalization application component.yii\base\Application
$idstringAn ID that uniquely identifies this module among other modules which have the same parent. Default: null.yii\base\Module
$languagestringThe language that is meant to be used for end users. Default: 'en-US'.yii\base\Application
$layoutstring|boolThe layout that should be applied for views in this application. Default: 'main'.yii\base\Application
$layoutPathstringThe root directory of layout files.yii\base\Module
$loadedModulesarrayList of loaded modules indexed by their class names. Default: [].yii\base\Application
$log read-onlyyii\log\DispatcherThe log dispatcher application component.yii\base\Application
$mailer read-onlyyii\mail\MailerInterfaceThe mailer application component.yii\base\Application
$memoryLimitintLimit in bytes, or PHP_INT_MAX for an unlimited PHP setting.Application
$moduleyii\base\Module|nullThe parent module of this module. null if this module does not have a parent. Default: null.yii\base\Module
$modulesarrayThe modules (indexed by their IDs).yii\base\Module
$namestringThe application name. Default: 'My Application'.yii\base\Application
$paramsarrayCustom module parameters (name => value). Default: [].yii\base\Module
$persistentComponentsarray<int, string>Lists component IDs that should stay alive across requests in long-running workers. Default: ['db', 'cache'].Application
$request read-onlyyii\web\RequestThe request component.yii\web\Application
$requestedActionyii\base\Action<yii\base\Controller>|nullThe requested Action. Default: null.yii\base\Application
$requestedParamsarray|nullThe parameters supplied to the requested action. Default: null.yii\base\Application
$requestedRoutestringThe requested route Default: null.yii\base\Application
$resetUploadedFilesboolRetained for configuration compatibility; request preparation always resets uploaded file state. Default: true.Application
$response read-onlyyii\web\ResponseThe response component.yii\web\Application
$runtimePathstringThe directory that stores runtime files.yii\base\Application
$security read-onlyyii\base\SecurityThe security application component.yii\base\Application
$session read-onlyyii\web\SessionThe session component.yii\web\Application
$sourceLanguagestringThe language that the application is written in. Default: 'en-US'.yii\base\Application
$stateintThe current application state during a request handling life cycle. Default: null.yii\base\Application
$syncCookieValidationboolControls whether cookie validation settings are synchronized between request and response. Default: true.Application
$timeZonestringThe time zone used by this application.yii\base\Application
$uniqueId read-onlystringThe unique ID of the module.yii\base\Application
$urlManager read-onlyyii\web\UrlManagerThe URL manager for this application.yii\base\Application
$user read-onlyyii\web\User<yii\web\TUserIdentity>The user component.yii\web\Application
$useSessionboolControls whether the session is opened before bootstrap and closed after it for each request. Default: true.Application
$vendorPathstringThe directory that stores vendor files.yii\base\Application
$versionstringDefines the application version string. Default: '0.3.0'.Application
$view read-onlyyii\base\View|yii\web\ViewThe view application component that is used to render various view files.yii\base\Application
$viewPathstringThe root directory of view files.yii\base\Module
MethodDescriptionDefined by
__construct()Initializes a new application instance.Application
__call()Calls the named method which is not a class method.yii\base\Component
__clone()This method is called after the object is created by cloning an existing one.yii\base\Component
__get()Getter magic method.yii\di\ServiceLocator
__isset()Checks if a property value is null.yii\di\ServiceLocator
__set()Sets the value of a component property.yii\base\Component
__unset()Sets a component property to be null.yii\base\Component
afterAction()This method is invoked right after an action within this module is executed.yii\base\Module
attachBehavior()Attaches a behavior to this component.yii\base\Component
attachBehaviors()Attaches a list of behaviors to the component.yii\base\Component
beforeAction()This method is invoked right before an action within this module is executed.yii\base\Module
behaviors()Returns a list of behaviors that this component should behave as.yii\base\Component
canGetProperty()Returns a value indicating whether a property can be read.yii\base\Component
canSetProperty()Returns a value indicating whether a property can be set.yii\base\Component
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
clean()Runs garbage collection and reports whether allocated memory reached 90% of the configured limit.Application
clear()Removes the component from the locator.yii\di\ServiceLocator
coreComponents()Returns Yii core components with bridge request, response, session, and error handler definitions.Application
createController()Creates a controller instance based on the given route.yii\base\Module
createControllerByID()Creates a controller based on the given controller ID.yii\base\Module
detachBehavior()Detaches a behavior from the component.yii\base\Component
detachBehaviors()Detaches all behaviors from the component.yii\base\Component
end()Terminates the application.yii\base\Application
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.yii\base\Component
finalize()Completes after-send processing and worker cleanup following the runtime’s emission attempt.Application
get()Returns the component instance with the specified ID.yii\base\Module
getAssetManager()Returns the asset manager.yii\base\Application
getAuthManager()Returns the auth manager for this application.yii\base\Application
getBasePath()Returns the root directory of the module.yii\base\Module
getBehavior()Returns the named behavior object.yii\base\Component
getBehaviors()Returns all behaviors attached to this component.yii\base\Component
getCache()Returns the cache component.yii\base\Application
getComponents()Returns the list of the component definitions or the loaded component instances.yii\di\ServiceLocator
getControllerPath()Returns the directory that contains the controller classes according to $controllerNamespace.yii\base\Module
getDb()Returns the database connection component.yii\base\Application
getErrorHandler()Returns the error handler component.yii\web\Application
getFormatter()Returns the formatter component.yii\base\Application
getHomeUrl()yii\web\Application
getI18n()Returns the internationalization (i18n) component.yii\base\Application
getInstance() staticReturns the currently requested instance of this module class.yii\base\Module
getLayoutPath()Returns the directory that contains layout view files for this module.yii\base\Module
getLog()Returns the log dispatcher component.yii\base\Application
getMailer()Returns the mailer component.yii\base\Application
getMemoryLimit()Returns the cached memory limit, parsing PHP’s setting when needed.Application
getModule()Retrieves the child module of the specified ID.yii\base\Module
getModules()Returns the sub-modules in this module.yii\base\Module
getRequest()Returns the request component.yii\web\Application
getResponse()Returns the response component.yii\web\Application
getRuntimePath()Returns the directory that stores runtime files.yii\base\Application
getSecurity()Returns the security component.yii\base\Application
getSession()Returns the session component.yii\web\Application
getTimeZone()Returns the time zone used by this application.yii\base\Application
getUniqueId()Returns an ID that uniquely identifies this module among all modules within the current application.yii\base\Application
getUrlManager()Returns the URL manager for this application.yii\base\Application
getUser()Returns the user component.yii\web\Application
getVendorPath()Returns the directory that stores vendor files.yii\base\Application
getVersion()Returns current module version.yii\base\Module
getView()Returns the view object.yii\base\Application
getViewPath()Returns the directory that contains the view files for this module.yii\base\Module
handle()Handles a PSR-7 request and returns the prepared response before emission.Application
handleRequest()Handles the specified request.yii\web\Application
has()Returns a value indicating whether the locator has the specified component definition or has instantiated the component.yii\base\Module
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.yii\base\Component
hasMethod()Returns a value indicating whether a method is defined.yii\base\Component
hasModule()Checks whether the child module of the specified ID exists.yii\base\Module
hasProperty()Returns a value indicating whether a property is defined for this component.yii\base\Component
init()Sets the per-request state to STATE_INIT; bootstrap runs after the PSR request is attached.Application
off()Detaches an existing event handler from this component.yii\base\Component
on()Attaches an event handler to an event.yii\base\Component
preInit()Pre-initializes the application.yii\base\Application
run()Runs the application.yii\base\Application
runAction()Runs a controller action specified by a route.yii\base\Module
set()Registers a component definition with this locator.yii\di\ServiceLocator
setAliases()Defines path aliases.yii\base\Module
setBasePath()Sets the root directory of the application and the @app alias.yii\base\Application
setComponents()Registers a set of component definitions in this locator.yii\di\ServiceLocator
setContainer()Configures Yii::$container with the $config.yii\base\Application
setControllerPath()Sets the directory that contains the controller classes.yii\base\Module
setHomeUrl()yii\web\Application
setInstance() staticSets the currently requested instance of this module class.yii\base\Module
setLayoutPath()Sets the directory that contains the layout files.yii\base\Module
setMemoryLimit()Overrides the cached worker memory threshold or requests recalculation from PHP’s setting.Application
setModule()Adds a sub-module to this module.yii\base\Module
setModules()Registers sub-modules in the current module.yii\base\Module
setRuntimePath()Sets the directory that stores runtime files.yii\base\Application
setTimeZone()Sets the time zone used by this application.yii\base\Application
setVendorPath()Sets the directory that stores vendor files.yii\base\Application
setVersion()Sets current module version.yii\base\Module
setViewPath()Sets the directory that contains the view files.yii\base\Module
trigger()Triggers an event.yii\base\Component
attachPsrRequest() protectedAttaches the PSR-7 request to the Yii request adapter.Application
bootstrap() protectedInitializes extensions and executes bootstrap components.yii\web\Application
createStandaloneAction() protectedResolves a route to a standalone Action class through convention-based discovery under $actionNamespace.yii\base\Module
defaultVersion() protectedReturns default module version.yii\base\Module
finalizeSessionState() protectedCloses the session after bootstrap completes.Application
openSessionFromRequestCookies() protectedOpens the session before bootstrap when $useSession is true.Application
prepareErrorHandler() protectedResets the error handler response target for the current request.Application
prepareForRequest() protectedPrepares the application state for a new PSR-7 request.Application
registerErrorHandler() protectedRegisters the errorHandler component as a PHP error handler.yii\base\Application
reinitializeApplication() protectedReinitializes the application instance for the current request.Application
resetRequestState() protectedResets route and action resolution state.Application
resetUploadedFilesState() protectedResets uploaded file static state for the current request.Application
runMappedAction() protectedRuns a standalone action registered in $actionMap.yii\base\Module
runStandaloneAction() protectedRuns a standalone Action instance through the standard module/action event lifecycle.yii\base\Module
syncCookieValidationState() protectedSynchronizes cookie validation settings between request and response.Application
syncSessionIdFromRequestCookies() protectedIsolates PHP session state for the current request without opening the session.Application
terminate() protectedConverts the Yii response to PSR-7 and stores it for deferred after-send finalization.Application
EventTypeDescriptionDefined by
EVENT_BEFORE_REQUESTyii\base\EventAn event raised before the application starts to handle a request. Event name: 'beforeRequest'.yii\base\Application
EVENT_AFTER_REQUESTyii\base\EventAn event raised after the application successfully handles a request (before the response is sent out). Event name: 'afterRequest'.yii\base\Application
EVENT_BEFORE_ACTIONyii\base\ActionEventAn event raised before executing a controller action. Event name: 'beforeAction'.yii\base\Module
EVENT_AFTER_ACTIONyii\base\ActionEventAn event raised after executing a controller action. Event name: 'afterAction'.yii\base\Module
ConstantValueDescriptionDefined by
STATE_BEGIN0Application state used by $state: application just started.yii\base\Application
STATE_INIT1Application state used by $state: application is initializing.yii\base\Application
STATE_BEFORE_REQUEST2Application state used by $state: application is triggering EVENT_BEFORE_REQUEST.yii\base\Application
STATE_HANDLING_REQUEST3Application state used by $state: application is handling the request.yii\base\Application
STATE_AFTER_REQUEST4Application state used by $state: application is triggering EVENT_AFTER_REQUEST.yii\base\Application
STATE_SENDING_RESPONSE5Application state used by $state: application is about to send response.yii\base\Application
STATE_END6Application state used by $state: application has ended.yii\base\Application
public array<int, string> $persistentComponents = ['db', 'cache']

Lists component IDs that should stay alive across requests in long-running workers.

Keep request-sensitive components (such as request or response) out of this list.

public bool $resetUploadedFiles = true

Retained for configuration compatibility; request preparation always resets uploaded file state.

Setting this property to false does not disable the reset.

public bool $syncCookieValidation = true

Controls whether cookie validation settings are synchronized between request and response.

Set to false to skip request-to-response cookie validation key synchronization.

public bool $useSession = true

Controls whether the session is opened before bootstrap and closed after it for each request.

The bridge always isolates PHP session state between requests through syncSessionIdFromRequestCookies(): it closes a session left active and applies the session ID from the request cookie (or an empty ID when the cookie is missing or malformed), so a lazily opened session belongs to the current visitor. Set to false to skip only the eager openSessionFromRequestCookies() and finalizeSessionState() hooks.

public function __construct(mixed[] $config = [])

Initializes a new application instance.

Parameters

ParameterTypeDescription
$configmixed[]Application configuration.
public function clean(): bool

Runs garbage collection and reports whether allocated memory reached 90% of the configured limit.

The runtime decides whether to recycle the worker. An unlimited PHP memory setting maps to PHP_INT_MAX.

Returns

bool: Whether allocated memory is at least 90% of the limit.

public function coreComponents(): mixed[]

Returns Yii core components with bridge request, response, session, and error handler definitions.

Returns

mixed[]: Core component definitions.

public function finalize(bool $success = true): void

Completes after-send processing and worker cleanup following the runtime’s emission attempt.

On success, calls Response::completeSend() on the response returned by handle(). Pass false when emission failed to skip after-send. Tracked event cleanup runs even if an after-send handler throws; logger flushing runs when enabled and a converted response is available.

Call exactly once per emission attempt. Keep this call outside an emitter catch block so an after-send exception is not treated as an emission failure and does not cause a second call.

Parameters

ParameterTypeDescription
$successboolWhether emission completed successfully.

Throws

  • Throwable: when an after-send handler, event cleanup, or logger flush fails.
public function getMemoryLimit(): int

Returns the cached memory limit, parsing PHP’s setting when needed.

Returns

int: Limit in bytes, or PHP_INT_MAX for an unlimited PHP setting.

public function handle(Psr\Http\Message\ServerRequestInterface $request): Psr\Http\Message\ResponseInterface

Handles a PSR-7 request and returns the prepared response before emission.

The runtime must emit this response and call finalize(). Application and request state are reinitialized before handling; bootstrap observes the current request’s session state.

Parameters

ParameterTypeDescription
$requestPsr\Http\Message\ServerRequestInterfaceRequest to process.

Returns

Psr\Http\Message\ResponseInterface: Prepared response; after-send processing remains deferred.

Throws

  • Throwable: When initialization, error handling, or response conversion cannot produce a response.
public function init(): void

Sets the per-request state to STATE_INIT; bootstrap runs after the PSR request is attached.

public function setMemoryLimit(int $limit): void

Overrides the cached worker memory threshold or requests recalculation from PHP’s setting.

Parameters

ParameterTypeDescription
$limitintLimit in bytes; a non-positive value recalculates on the next getMemoryLimit() call.
protected function attachPsrRequest(Psr\Http\Message\ServerRequestInterface $request): void

Attaches the PSR-7 request to the Yii request adapter.

Parameters

ParameterTypeDescription
$requestPsr\Http\Message\ServerRequestInterfaceRequest to attach.
protected function finalizeSessionState(): void

Closes the session after bootstrap completes.

protected function openSessionFromRequestCookies(): void

Opens the session before bootstrap when $useSession is true.

Runs after syncSessionIdFromRequestCookies(), so the session resumes the ID from the request cookie or starts with a new one.

protected function prepareErrorHandler(): void

Resets the error handler response target for the current request.

protected function prepareForRequest(Psr\Http\Message\ServerRequestInterface $request): void

Prepares the application state for a new PSR-7 request.

Parameters

ParameterTypeDescription
$requestPsr\Http\Message\ServerRequestInterfaceRequest to bind to the Yii adapter.

Throws

protected function reinitializeApplication(): void

Reinitializes the application instance for the current request.

Throws

protected function resetRequestState(): void

Resets route and action resolution state.

protected function resetUploadedFilesState(): void

Resets uploaded file static state for the current request.

protected function syncCookieValidationState(): void

Synchronizes cookie validation settings between request and response.

protected function syncSessionIdFromRequestCookies(): void

Isolates PHP session state for the current request without opening the session.

Runs for every request, regardless of $useSession: closes a session left active by a previous request and applies the session ID from the request cookie, or an empty ID when the request carries no session cookie, so a later session_start() resumes the current visitor’s session or creates a new one. Request cookies are read only when the session cookie is present. A malformed cookie value (not a string, or not accepted by PHP as a session ID) is discarded and an empty ID is applied instead, so the session starts with a new ID.

protected function terminate(yii2\extensions\psrbridge\http\Response $response): Psr\Http\Message\ResponseInterface

Converts the Yii response to PSR-7 and stores it for deferred after-send finalization.

Runs the pre-send conversion only. After-send finalization, event cleanup, and logger flushing are deferred to finalize(), which the runtime calls once the PSR-7 response has been emitted.

Parameters

ParameterTypeDescription
$responseResponseYii response to convert.

Returns

Psr\Http\Message\ResponseInterface: PSR-7 response produced by the conversion.

Throws