class Application extends Container implements HttpKernelInterface, TerminableInterface

The Silex framework class.

Constants

VERSION

EARLY_EVENT

LATE_EVENT

Properties

protected $providers
protected $booted

Methods

__construct(array $values = array())

Instantiate a new Application.

register(ServiceProviderInterface $provider, array $values = array())

Registers a service provider.

boot()

Boots all service providers.

match(string $pattern, mixed $to = null)

Maps a pattern to a callable.

get(string $pattern, mixed $to = null)

Maps a GET request to a callable.

post(string $pattern, mixed $to = null)

Maps a POST request to a callable.

put(string $pattern, mixed $to = null)

Maps a PUT request to a callable.

delete(string $pattern, mixed $to = null)

Maps a DELETE request to a callable.

options(string $pattern, mixed $to = null)

Maps an OPTIONS request to a callable.

patch(string $pattern, mixed $to = null)

Maps a PATCH request to a callable.

on(string $eventName, callable $callback, int $priority)

Adds an event listener that listens on the specified events.

before(mixed $callback, int $priority)

Registers a before filter.

after(mixed $callback, int $priority)

Registers an after filter.

finish(mixed $callback, int $priority)

Registers a finish filter.

abort(int $statusCode, string $message = '', array $headers = array())

Aborts the current request by sending a proper HTTP error.

error(mixed $callback, int $priority = -8)

Registers an error handler.

view(mixed $callback, int $priority)

Registers a view handler.

flush()

Flushes the controller collection.

RedirectResponse
redirect(string $url, int $status = 302)

Redirects the user to another URL.

StreamedResponse
stream(mixed $callback = null, int $status = 200, array $headers = array())

Creates a streaming response.

string
escape(string $text, int $flags = ENT_COMPAT, string $charset = null, bool $doubleEncode = true)

Escapes a text for HTML.

JsonResponse
json(mixed $data = array(), int $status = 200, array $headers = array())

Convert some data into a JSON response.

BinaryFileResponse
sendFile(SplFileInfo|string $file, int $status = 200, array $headers = array(), null|string $contentDisposition = null)

Sends a file.

mount(string $prefix, ControllerCollection|callable|ControllerProviderInterface $controllers)

Mounts controllers under the given route prefix.

run(Request $request = null)

Handles the request and delivers the response.

handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)

{@inheritdoc}

terminate(Request $request, Response $response)

{@inheritdoc}

Details

at line 59
__construct(array $values = array())

Instantiate a new Application.

Objects and parameters can be passed as argument to the constructor.

Parameters

array $values The parameters or objects.

at line 86
Application register(ServiceProviderInterface $provider, array $values = array())

Registers a service provider.

Parameters

ServiceProviderInterface $provider A ServiceProviderInterface instance
array $values An array of values that customizes the provider

Return Value

Application

at line 101
boot()

Boots all service providers.

This method is automatically called by handle(), but you can use it to boot all service providers when not handling a request.

at line 130
Controller match(string $pattern, mixed $to = null)

Maps a pattern to a callable.

You can optionally specify HTTP methods that should be matched.

Parameters

string $pattern Matched route pattern
mixed $to Callback that returns the response when matched

Return Value

Controller

at line 143
Controller get(string $pattern, mixed $to = null)

Maps a GET request to a callable.

Parameters

string $pattern Matched route pattern
mixed $to Callback that returns the response when matched

Return Value

Controller

at line 156
Controller post(string $pattern, mixed $to = null)

Maps a POST request to a callable.

Parameters

string $pattern Matched route pattern
mixed $to Callback that returns the response when matched

Return Value

Controller

at line 169
Controller put(string $pattern, mixed $to = null)

Maps a PUT request to a callable.

Parameters

string $pattern Matched route pattern
mixed $to Callback that returns the response when matched

Return Value

Controller

at line 182
Controller delete(string $pattern, mixed $to = null)

Maps a DELETE request to a callable.

Parameters

string $pattern Matched route pattern
mixed $to Callback that returns the response when matched

Return Value

Controller

at line 195
Controller options(string $pattern, mixed $to = null)

Maps an OPTIONS request to a callable.

Parameters

string $pattern Matched route pattern
mixed $to Callback that returns the response when matched

Return Value

Controller

at line 208
Controller patch(string $pattern, mixed $to = null)

Maps a PATCH request to a callable.

Parameters

string $pattern Matched route pattern
mixed $to Callback that returns the response when matched

Return Value

Controller

at line 221
on(string $eventName, callable $callback, int $priority)

Adds an event listener that listens on the specified events.

Parameters

string $eventName The event to listen on
callable $callback The listener
int $priority The higher this value, the earlier an event listener will be triggered in the chain (defaults to 0)

at line 245
before(mixed $callback, int $priority)

Registers a before filter.

Before filters are run before any route has been matched.

Parameters

mixed $callback Before filter callback
int $priority The higher this value, the earlier an event listener will be triggered in the chain (defaults to 0)

at line 271
after(mixed $callback, int $priority)

Registers an after filter.

After filters are run after the controller has been executed.

Parameters

mixed $callback After filter callback
int $priority The higher this value, the earlier an event listener will be triggered in the chain (defaults to 0)

at line 298
finish(mixed $callback, int $priority)

Registers a finish filter.

Finish filters are run after the response has been sent.

Parameters

mixed $callback Finish filter callback
int $priority The higher this value, the earlier an event listener will be triggered in the chain (defaults to 0)

at line 314
abort(int $statusCode, string $message = '', array $headers = array())

Aborts the current request by sending a proper HTTP error.

Parameters

int $statusCode The HTTP status code
string $message The status message
array $headers An array of HTTP headers

at line 336
error(mixed $callback, int $priority = -8)

Registers an error handler.

Error handlers are simple callables which take a single Exception as an argument. If a controller throws an exception, an error handler can return a specific response.

When an exception occurs, all handlers will be called, until one returns something (a string or a Response object), at which point that will be returned to the client.

For this reason you should add logging handlers before output handlers.

Parameters

mixed $callback Error handler callback, takes an Exception argument
int $priority The higher this value, the earlier an event listener will be triggered in the chain (defaults to -8)

at line 353
view(mixed $callback, int $priority)

Registers a view handler.

View handlers are simple callables which take a controller result and the request as arguments, whenever a controller returns a value that is not an instance of Response. When this occurs, all suitable handlers will be called, until one returns a Response object.

Parameters

mixed $callback View handler callback
int $priority The higher this value, the earlier an event listener will be triggered in the chain (defaults to 0)

at line 361
flush()

Flushes the controller collection.

at line 374
RedirectResponse redirect(string $url, int $status = 302)

Redirects the user to another URL.

Parameters

string $url The URL to redirect to
int $status The status code (302 by default)

Return Value

RedirectResponse

at line 388
StreamedResponse stream(mixed $callback = null, int $status = 200, array $headers = array())

Creates a streaming response.

Parameters

mixed $callback A valid PHP callback
int $status The response status code
array $headers An array of response headers

Return Value

StreamedResponse

at line 403
string escape(string $text, int $flags = ENT_COMPAT, string $charset = null, bool $doubleEncode = true)

Escapes a text for HTML.

Parameters

string $text The input text to be escaped
int $flags The flags (see htmlspecialchars)
string $charset The charset
bool $doubleEncode Whether to try to avoid double escaping or not

Return Value

string Escaped text

at line 417
JsonResponse json(mixed $data = array(), int $status = 200, array $headers = array())

Convert some data into a JSON response.

Parameters

mixed $data The response data
int $status The response status code
array $headers An array of response headers

Return Value

JsonResponse

at line 432
BinaryFileResponse sendFile(SplFileInfo|string $file, int $status = 200, array $headers = array(), null|string $contentDisposition = null)

Sends a file.

Parameters

SplFileInfo|string $file The file to stream
int $status The response status code
array $headers An array of response headers
null|string $contentDisposition The type of Content-Disposition to set automatically with the filename

Return Value

BinaryFileResponse

at line 447
Application mount(string $prefix, ControllerCollection|callable|ControllerProviderInterface $controllers)

Mounts controllers under the given route prefix.

Parameters

string $prefix The route prefix
ControllerCollection|callable|ControllerProviderInterface $controllers A ControllerCollection, a callable, or a ControllerProviderInterface instance

Return Value

Application

Exceptions

LogicException

at line 471
run(Request $request = null)

Handles the request and delivers the response.

Parameters

Request $request Request to process

at line 488
handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)

{@inheritdoc}

If you call this method directly instead of run(), you must call the terminate() method yourself if you want the finish filters to be run.

Parameters

Request $request
$type
$catch

at line 502
terminate(Request $request, Response $response)

{@inheritdoc}

Parameters

Request $request
Response $response