Application
API reference › PSR bridge › http › Application
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.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$actionMap | array<string, | Mapping from action IDs to standalone Action class configurations. Default: []. Available since 22.0. | yii\ |
$actionNamespace | string| | The namespace where standalone Action subclasses are located for convention-based discovery, parallel to $controllerNamespace for controllers. Default: null. Available since 22.0. | yii\ |
$aliases write-only | array | List of path aliases to be defined. | yii\ |
$assetManager read-only | yii\ | The asset manager application component. | yii\ |
$authManager read-only | yii\| | The auth manager application component or null if it’s not configured. | yii\ |
$basePath | string | The root directory of the application. | yii\ |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$bootstrap | array | List of components that should be run during the application bootstrapping process. Default: []. | yii\ |
$cache read-only | yii\| | The cache application component. | yii\ |
$catchAll | array| | The configuration specifying a controller action which should handle all user requests. Default: null. | yii\ |
$charset | string | The charset currently used for the application. Default: 'UTF-8'. | yii\ |
$components | array | The list of the component definitions or the loaded component instances (ID => definition or instance). | yii\ |
$container write-only | array | Values given in terms of name-value pairs. | yii\ |
$controller | yii\| | The currently active controller instance Default: null. | yii\ |
$controllerMap | array | Mapping from controller ID to controller configurations. Default: []. | yii\ |
$controllerNamespace | string | The namespace that controller classes are located in. Default: 'app\. | yii\ |
$controllerPath | string | The directory that contains the controller classes. | yii\ |
$db read-only | yii\ | The database connection. | yii\ |
$defaultRoute | string | The default route of this application. Default: 'site'. | yii\ |
$errorHandler read-only | yii\ | The error handler application component. | yii\ |
$extensions | array| | List of installed Yii extensions. Default: null. | yii\ |
$flushLogger | bool | Flushes the logger during finalize() when set to true. Default: true. | Application |
$formatter read-only | yii\ | The formatter application component. | yii\ |
$homeUrl | string | The homepage URL. | yii\ |
$i18n read-only | yii\ | The internationalization application component. | yii\ |
$id | string | An ID that uniquely identifies this module among other modules which have the same parent. Default: null. | yii\ |
$language | string | The language that is meant to be used for end users. Default: 'en-US'. | yii\ |
$layout | string| | The layout that should be applied for views in this application. Default: 'main'. | yii\ |
$layoutPath | string | The root directory of layout files. | yii\ |
$loadedModules | array | List of loaded modules indexed by their class names. Default: []. | yii\ |
$log read-only | yii\ | The log dispatcher application component. | yii\ |
$mailer read-only | yii\ | The mailer application component. | yii\ |
$memoryLimit | int | Limit in bytes, or PHP_INT_MAX for an unlimited PHP setting. | Application |
$module | yii\| | The parent module of this module. null if this module does not have a parent. Default: null. | yii\ |
$modules | array | The modules (indexed by their IDs). | yii\ |
$name | string | The application name. Default: 'My Application'. | yii\ |
$params | array | Custom module parameters (name => value). Default: []. | yii\ |
$persistentComponents | array<int, | Lists component IDs that should stay alive across requests in long-running workers. Default: ['db',. | Application |
$request read-only | yii\ | The request component. | yii\ |
$requestedAction | yii\<yii\>| | The requested Action. Default: null. | yii\ |
$requestedParams | array| | The parameters supplied to the requested action. Default: null. | yii\ |
$requestedRoute | string | The requested route Default: null. | yii\ |
$resetUploadedFiles | bool | Retained for configuration compatibility; request preparation always resets uploaded file state. Default: true. | Application |
$response read-only | yii\ | The response component. | yii\ |
$runtimePath | string | The directory that stores runtime files. | yii\ |
$security read-only | yii\ | The security application component. | yii\ |
$session read-only | yii\ | The session component. | yii\ |
$sourceLanguage | string | The language that the application is written in. Default: 'en-US'. | yii\ |
$state | int | The current application state during a request handling life cycle. Default: null. | yii\ |
$syncCookieValidation | bool | Controls whether cookie validation settings are synchronized between request and response. Default: true. | Application |
$timeZone | string | The time zone used by this application. | yii\ |
$uniqueId read-only | string | The unique ID of the module. | yii\ |
$urlManager read-only | yii\ | The URL manager for this application. | yii\ |
$user read-only | yii\<yii\ | The user component. | yii\ |
$useSession | bool | Controls whether the session is opened before bootstrap and closed after it for each request. Default: true. | Application |
$vendorPath | string | The directory that stores vendor files. | yii\ |
$version | string | Defines the application version string. Default: '0.3.0'. | Application |
$view read-only | yii\|yii\ | The view application component that is used to render various view files. | yii\ |
$viewPath | string | The root directory of view files. | yii\ |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Initializes a new application instance. | Application |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Getter magic method. | yii\ |
__isset() | Checks if a property value is null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
afterAction() | This method is invoked right after an action within this module is executed. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
beforeAction() | This method is invoked right before an action within this module is executed. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
clean() | Runs garbage collection and reports whether allocated memory reached 90% of the configured limit. | Application |
clear() | Removes the component from the locator. | yii\ |
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\ |
createControllerByID() | Creates a controller based on the given controller ID. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
end() | Terminates the application. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
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\ |
getAssetManager() | Returns the asset manager. | yii\ |
getAuthManager() | Returns the auth manager for this application. | yii\ |
getBasePath() | Returns the root directory of the module. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getCache() | Returns the cache component. | yii\ |
getComponents() | Returns the list of the component definitions or the loaded component instances. | yii\ |
getControllerPath() | Returns the directory that contains the controller classes according to $controllerNamespace. | yii\ |
getDb() | Returns the database connection component. | yii\ |
getErrorHandler() | Returns the error handler component. | yii\ |
getFormatter() | Returns the formatter component. | yii\ |
getHomeUrl() | yii\ | |
getI18n() | Returns the internationalization (i18n) component. | yii\ |
getInstance() static | Returns the currently requested instance of this module class. | yii\ |
getLayoutPath() | Returns the directory that contains layout view files for this module. | yii\ |
getLog() | Returns the log dispatcher component. | yii\ |
getMailer() | Returns the mailer component. | yii\ |
getMemoryLimit() | Returns the cached memory limit, parsing PHP’s setting when needed. | Application |
getModule() | Retrieves the child module of the specified ID. | yii\ |
getModules() | Returns the sub-modules in this module. | yii\ |
getRequest() | Returns the request component. | yii\ |
getResponse() | Returns the response component. | yii\ |
getRuntimePath() | Returns the directory that stores runtime files. | yii\ |
getSecurity() | Returns the security component. | yii\ |
getSession() | Returns the session component. | yii\ |
getTimeZone() | Returns the time zone used by this application. | yii\ |
getUniqueId() | Returns an ID that uniquely identifies this module among all modules within the current application. | yii\ |
getUrlManager() | Returns the URL manager for this application. | yii\ |
getUser() | Returns the user component. | yii\ |
getVendorPath() | Returns the directory that stores vendor files. | yii\ |
getVersion() | Returns current module version. | yii\ |
getView() | Returns the view object. | yii\ |
getViewPath() | Returns the directory that contains the view files for this module. | yii\ |
handle() | Handles a PSR-7 request and returns the prepared response before emission. | Application |
handleRequest() | Handles the specified request. | yii\ |
has() | Returns a value indicating whether the locator has the specified component definition or has instantiated the component. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasModule() | Checks whether the child module of the specified ID exists. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
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\ |
on() | Attaches an event handler to an event. | yii\ |
preInit() | Pre-initializes the application. | yii\ |
run() | Runs the application. | yii\ |
runAction() | Runs a controller action specified by a route. | yii\ |
set() | Registers a component definition with this locator. | yii\ |
setAliases() | Defines path aliases. | yii\ |
setBasePath() | Sets the root directory of the application and the @app alias. | yii\ |
setComponents() | Registers a set of component definitions in this locator. | yii\ |
setContainer() | Configures Yii::$container with the $config. | yii\ |
setControllerPath() | Sets the directory that contains the controller classes. | yii\ |
setHomeUrl() | yii\ | |
setInstance() static | Sets the currently requested instance of this module class. | yii\ |
setLayoutPath() | Sets the directory that contains the layout files. | yii\ |
setMemoryLimit() | Overrides the cached worker memory threshold or requests recalculation from PHP’s setting. | Application |
setModule() | Adds a sub-module to this module. | yii\ |
setModules() | Registers sub-modules in the current module. | yii\ |
setRuntimePath() | Sets the directory that stores runtime files. | yii\ |
setTimeZone() | Sets the time zone used by this application. | yii\ |
setVendorPath() | Sets the directory that stores vendor files. | yii\ |
setVersion() | Sets current module version. | yii\ |
setViewPath() | Sets the directory that contains the view files. | yii\ |
trigger() | Triggers an event. | yii\ |
attachPsrRequest() protected | Attaches the PSR-7 request to the Yii request adapter. | Application |
bootstrap() protected | Initializes extensions and executes bootstrap components. | yii\ |
createStandaloneAction() protected | Resolves a route to a standalone Action class through convention-based discovery under $actionNamespace. | yii\ |
defaultVersion() protected | Returns default module version. | yii\ |
finalizeSessionState() protected | Closes the session after bootstrap completes. | Application |
openSessionFromRequestCookies() protected | Opens the session before bootstrap when $useSession is true. | Application |
prepareErrorHandler() protected | Resets the error handler response target for the current request. | Application |
prepareForRequest() protected | Prepares the application state for a new PSR-7 request. | Application |
registerErrorHandler() protected | Registers the errorHandler component as a PHP error handler. | yii\ |
reinitializeApplication() protected | Reinitializes the application instance for the current request. | Application |
resetRequestState() protected | Resets route and action resolution state. | Application |
resetUploadedFilesState() protected | Resets uploaded file static state for the current request. | Application |
runMappedAction() protected | Runs a standalone action registered in $actionMap. | yii\ |
runStandaloneAction() protected | Runs a standalone Action instance through the standard module/action event lifecycle. | yii\ |
syncCookieValidationState() protected | Synchronizes cookie validation settings between request and response. | Application |
syncSessionIdFromRequestCookies() protected | Isolates PHP session state for the current request without opening the session. | Application |
terminate() protected | Converts the Yii response to PSR-7 and stores it for deferred after-send finalization. | Application |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_BEFORE_REQUEST | yii\ | An event raised before the application starts to handle a request. Event name: 'beforeRequest'. | yii\ |
EVENT_AFTER_REQUEST | yii\ | An event raised after the application successfully handles a request (before the response is sent out). Event name: 'afterRequest'. | yii\ |
EVENT_BEFORE_ACTION | yii\ | An event raised before executing a controller action. Event name: 'beforeAction'. | yii\ |
EVENT_AFTER_ACTION | yii\ | An event raised after executing a controller action. Event name: 'afterAction'. | yii\ |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
STATE_BEGIN | 0 | Application state used by $state: application just started. | yii\ |
STATE_INIT | 1 | Application state used by $state: application is initializing. | yii\ |
STATE_BEFORE_REQUEST | 2 | Application state used by $state: application is triggering EVENT_BEFORE_REQUEST. | yii\ |
STATE_HANDLING_REQUEST | 3 | Application state used by $state: application is handling the request. | yii\ |
STATE_AFTER_REQUEST | 4 | Application state used by $state: application is triggering EVENT_AFTER_REQUEST. | yii\ |
STATE_SENDING_RESPONSE | 5 | Application state used by $state: application is about to send response. | yii\ |
STATE_END | 6 | Application state used by $state: application has ended. | yii\ |
Property details
Section titled “Property details”$persistentComponents
Section titled “$persistentComponents ”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.
$resetUploadedFiles
Section titled “$resetUploadedFiles ”public bool $resetUploadedFiles = trueRetained for configuration compatibility; request preparation always resets uploaded file state.
Setting this property to false does not disable the reset.
$syncCookieValidation
Section titled “$syncCookieValidation ”public bool $syncCookieValidation = trueControls whether cookie validation settings are synchronized between request and response.
Set to false to skip request-to-response cookie validation key synchronization.
$useSession
Section titled “$useSession ”public bool $useSession = trueControls 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.
Method details
Section titled “Method details”__construct()
Section titled “__construct() ”public function __construct(mixed[] $config = [])Initializes a new application instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
$config | mixed[] | Application configuration. |
clean()
Section titled “clean() ”public function clean(): boolRuns 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.
coreComponents()
Section titled “coreComponents() ”public function coreComponents(): mixed[]Returns Yii core components with bridge request, response, session, and error handler definitions.
Returns
mixed[]: Core component definitions.
finalize()
Section titled “finalize() ”public function finalize(bool $success = true): voidCompletes 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
| Parameter | Type | Description |
|---|---|---|
$success | bool | Whether emission completed successfully. |
Throws
Throwable: when an after-send handler, event cleanup, or logger flush fails.
getMemoryLimit()
Section titled “getMemoryLimit() ”public function getMemoryLimit(): intReturns the cached memory limit, parsing PHP’s setting when needed.
Returns
int: Limit in bytes, or PHP_INT_MAX for an unlimited PHP setting.
handle()
Section titled “handle() ”public function handle(Psr\Http\Message\ServerRequestInterface $request): Psr\Http\Message\ResponseInterfaceHandles 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
| Parameter | Type | Description |
|---|---|---|
$request | Psr\ | Request 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.
init()
Section titled “init() ”public function init(): voidSets the per-request state to STATE_INIT; bootstrap runs after the PSR request is attached.
setMemoryLimit()
Section titled “setMemoryLimit() ”public function setMemoryLimit(int $limit): voidOverrides the cached worker memory threshold or requests recalculation from PHP’s setting.
Parameters
| Parameter | Type | Description |
|---|---|---|
$limit | int | Limit in bytes; a non-positive value recalculates on the next getMemoryLimit() call. |
attachPsrRequest()
Section titled “attachPsrRequest() ”protected function attachPsrRequest(Psr\Http\Message\ServerRequestInterface $request): voidAttaches the PSR-7 request to the Yii request adapter.
Parameters
| Parameter | Type | Description |
|---|---|---|
$request | Psr\ | Request to attach. |
finalizeSessionState()
Section titled “finalizeSessionState() ”protected function finalizeSessionState(): voidCloses the session after bootstrap completes.
openSessionFromRequestCookies()
Section titled “openSessionFromRequestCookies() ”protected function openSessionFromRequestCookies(): voidOpens 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.
prepareErrorHandler()
Section titled “prepareErrorHandler() ”protected function prepareErrorHandler(): voidResets the error handler response target for the current request.
prepareForRequest()
Section titled “prepareForRequest() ”protected function prepareForRequest(Psr\Http\Message\ServerRequestInterface $request): voidPrepares the application state for a new PSR-7 request.
Parameters
| Parameter | Type | Description |
|---|---|---|
$request | Psr\ | Request to bind to the Yii adapter. |
Throws
yii\base\InvalidConfigException: when application configuration is invalid.
reinitializeApplication()
Section titled “reinitializeApplication() ”protected function reinitializeApplication(): voidReinitializes the application instance for the current request.
Throws
yii\base\InvalidConfigException: when application configuration is invalid.
resetRequestState()
Section titled “resetRequestState() ”protected function resetRequestState(): voidResets route and action resolution state.
resetUploadedFilesState()
Section titled “resetUploadedFilesState() ”protected function resetUploadedFilesState(): voidResets uploaded file static state for the current request.
syncCookieValidationState()
Section titled “syncCookieValidationState() ”protected function syncCookieValidationState(): voidSynchronizes cookie validation settings between request and response.
syncSessionIdFromRequestCookies()
Section titled “syncSessionIdFromRequestCookies() ”protected function syncSessionIdFromRequestCookies(): voidIsolates 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.
terminate()
Section titled “terminate() ”protected function terminate(yii2\extensions\psrbridge\http\Response $response): Psr\Http\Message\ResponseInterfaceConverts 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
| Parameter | Type | Description |
|---|---|---|
$response | Response | Yii response to convert. |
Returns
Psr\Http\Message\ResponseInterface: PSR-7 response produced by the conversion.
Throws
yii\base\InvalidConfigException: when configuration is invalid.yii\di\NotInstantiableException: when a service cannot be instantiated.