RoutingContext

Represents the context for the handling of a request in Vert.x-Web.

A new instance is created for each HTTP request that is received in the of the router.

The same instance is passed to any matching request or failure handlers during the routing of the request or failure.

The context provides access to the and and allows you to maintain arbitrary data that lives for the lifetime of the context. Contexts are discarded once they have been routed to the handler for the request.

The context also provides access to the @see \io\vertx\jphp\ext\web\Session, cookies and body for the request, given the correct handlers in the application.

package

Default

Methods

__construct

__construct() 

Returns the languages for the current request. The languages are determined from the <code>Accept-Language</code> header and sorted on quality.

acceptableLanguages() : array

When 2 or more entries have the same quality then the order used to return the best match is based on the lowest index on the original list. For example if a user has en-US and en-GB with same quality and this order the best match will be en-US because it was declared as first entry by the client.

Response

array

The best matched language for the request

Returns the locales for the current request. The locales are determined from the `accept-languages` header and sorted on quality.

acceptableLocales() : array

When 2 or more entries have the same quality then the order used to return the best match is based on the lowest index on the original list. For example if a user has en-US and en-GB with same quality and this order the best match will be en-US because it was declared as first entry by the client.

deprecated

Response

array

the best matched locale for the request

Provides a handler that will be called after the last part of the body is written to the wire.

addBodyEndHandler( $arg0) : integer

The handler is called asynchronously of when the response has been received by the client. This provides a hook allowing you to do more operations once the request has been sent over the wire. Do not use this for resource cleanup as this handler might never get called (e.g. if the connection is reset).

Arguments

$arg0

callable

Response

integer

the id of the handler. This can be used if you later want to remove the handler.

Add a cookie. This will be sent back to the client in the response. The context must have first been routed to a @see \io\vertx\jphp\ext\web\handler\CookieHandler for this to work.

addCookie( $arg0) : $this

Arguments

$arg0

Cookie

Response

$this

a reference to this, so the API can be used fluently

Add a handler that will be called just before headers are written to the response. This gives you a hook where you can write any extra headers before the response has been written when it will be too late.

addHeadersEndHandler( $arg0) : integer

Arguments

$arg0

callable

Response

integer

the id of the handler. This can be used if you later want to remove the handler.

Clear the current user object in the context. This usually is used for implementing a log out feature, since the current user is unbounded from the routing context.

clearUser() : void

cookieCount

cookieCount() : integer

Response

integer

the number of cookies. The context must have first been routed to a {@link io.vertx.ext.web.handler.CookieHandler} for this to work.

cookies

cookies() : array

Response

array

a set of all the cookies. The context must have first been routed to a {@link io.vertx.ext.web.handler.CookieHandler} for this to be populated.

currentRoute

currentRoute() : \io\vertx\jphp\ext\web\Route

Response

\io\vertx\jphp\ext\web\Route

the current route this context is being routed through.

Fail the context with the specified status code.

fail( $arg0) : void

This will cause the router to route the context to any matching failure handlers for the request. If no failure handlers match a default failure response will be sent. param $statusCode [integer] the HTTP status code fail($statusCode) Fail the context with the specified throwable.

This will cause the router to route the context to any matching failure handlers for the request. If no failure handlers match a default failure response with status code 500 will be sent. param $throwable [\Exception] a throwable representing the failure fail($throwable)

Arguments

$arg0

\Exception | integer

failed

failed() : boolean

Response

boolean

true if the context is being routed to failure handlers.

If the context is being routed to failure handlers after a failure has been triggered by calling

failure() : \Exception
see \io\vertx\jphp\ext\web\RoutingContext::fail

then this will return that throwable. It can be used by failure handlers to render a response, e.g. create a failure response page.

Response

\Exception

the throwable used when signalling failure

fileUploads

fileUploads() : array

Response

array

a set of fileuploads (if any) for the request. The context must have first been routed to a {@link io.vertx.ext.web.handler.BodyHandler} for this to work.

Get some data from the context. The data is available in any handlers that receive the context.

get( $arg0) : mixed

Arguments

$arg0

string

Response

mixed

the data

If the route specifies produces matches, e.g. produces `text/html` and `text/plain`, and the `accept` header matches one or more of these then this returns the most acceptable match.

getAcceptableContentType() : string

Response

string

the most acceptable content type.

getBody

getBody() : \io\vertx\jphp\core\buffer\Buffer

Response

\io\vertx\jphp\core\buffer\Buffer

Get the entire HTTP request body as a {@link Buffer}. The context must have first been routed to a {@link io.vertx.ext.web.handler.BodyHandler} for this to be populated.

getBodyAsJson

getBodyAsJson() : array

Response

array

Get the entire HTTP request body as a {@link JsonObject}. The context must have first been routed to a {@link io.vertx.ext.web.handler.BodyHandler} for this to be populated.

getBodyAsJsonArray

getBodyAsJsonArray() : array

Response

array

Get the entire HTTP request body as a {@link JsonArray}. The context must have first been routed to a {@link io.vertx.ext.web.handler.BodyHandler} for this to be populated.

<b> getBodyAsString() </b>

getBodyAsString( $arg0 = null) : string

Get the entire HTTP request body as a string, assuming the specified encoding. The context must have first been routed to a

see \io\vertx\jphp\ext\web\handler\BodyHandler

for this to be populated.

param $encoding [string] the encoding, e.g. "UTF-16" getBodyAsString($encoding)

Arguments

$arg0

string

Response

string

the entire HTTP request body as a string, assuming UTF-8 encoding. The context must have first been routed to a {@link io.vertx.ext.web.handler.BodyHandler} for this to be populated.

Get the cookie with the specified name. The context must have first been routed to a @see \io\vertx\jphp\ext\web\handler\CookieHandler for this to work.

getCookie( $arg0) : \io\vertx\jphp\ext\web\Cookie

Arguments

$arg0

string

Response

\io\vertx\jphp\ext\web\Cookie

the cookie

mountPoint

mountPoint() : string

Response

string

the mount point for this router. It will be null for a top level router. For a sub-router it will be the path at which the subrouter was mounted.

Tell the router to route this context to the next matching route (if any).

next() : void

This method, if called, does not need to be called during the execution of the handler, it can be called some arbitrary time later, if required.

If next is not called for a handler then the handler should make sure it ends the response or no response will be sent.

Return the normalised path for the request.

normalisedPath() : string

The normalised path is where the URI path has been decoded, i.e. any unicode or other illegal URL characters that were encoded in the original URL with `%` will be returned to their original form. E.g. `%20` will revert to a space. Also `+` reverts to a space in a query.

The normalised path will also not contain any `..` character sequences to prevent resources being accessed outside of the permitted area.

It's recommended to always use the normalised path as opposed to if accessing server resources requested by a client.

Response

string

the normalised path

The headers: <ol> <li>Accept</li> <li>Accept-Charset</li> <li>Accept-Encoding</li> <li>Accept-Language</li> <li>Content-Type</li> </ol> Parsed into @see \io\vertx\jphp\ext\web\ParsedHeaderValue

parsedHeaders() : \io\vertx\jphp\ext\web\ParsedHeaderValues

Response

\io\vertx\jphp\ext\web\ParsedHeaderValues

A container with the parsed headers.

Gets the value of a single path parameter

pathParam( $arg0) : string

Arguments

$arg0

string

Response

string

the actual value of the parameter or null if it doesn't exist

Returns a map of named parameters as defined in path declaration with their actual values

pathParams() : array

Response

array

the map of named parameters

Helper to return the user preferred language.

preferredLanguage() : \io\vertx\jphp\ext\web\LanguageHeader

It is the same action as returning the first element of the acceptable languages.

Response

\io\vertx\jphp\ext\web\LanguageHeader

the users preferred locale.

Helper to return the user preferred locale. It is the same action as returning the first element of the acceptable locales.

preferredLocale() : \io\vertx\jphp\ext\web\Locale
deprecated

Response

\io\vertx\jphp\ext\web\Locale

the users preferred locale.

Put some arbitrary data in the context. This will be available in any handlers that receive the context.

put( $arg0,  $arg1) : $this

Arguments

$arg0

string

$arg1

mixed

Response

$this

a reference to this, so the API can be used fluently

Gets the value of a single query parameter

queryParam( $arg0) : array

Arguments

$arg0

string

Response

array

The list of all elements inside query parameter

Returns a map of all query parameters inside the <a href="https://en.wikipedia.org/wiki/Query_string">query string</a>

queryParams() : \io\vertx\jphp\core\MultiMap

Response

\io\vertx\jphp\core\MultiMap

the multimap of query parameters

Remove some data from the context. The data is available in any handlers that receive the context.

remove( $arg0) : mixed

Arguments

$arg0

string

Response

mixed

the previous data associated with the key

Remove a body end handler

removeBodyEndHandler( $arg0) : boolean

Arguments

$arg0

integer

Response

boolean

true if the handler existed and was removed, false otherwise

Expire a cookie, notifying a User Agent to remove it from its cookie jar. The context must have first been routed to a @see \io\vertx\jphp\ext\web\handler\CookieHandler for this to work.

removeCookie( $arg0,  $arg1 = null) : \io\vertx\jphp\ext\web\Cookie

param $name [string] the name of the cookie removeCookie($name)

Remove a cookie from the cookie set. If invalidate is true then it will expire a cookie, notifying a User Agent to remove it from its cookie jar. The context must have first been routed to a

see \io\vertx\jphp\ext\web\handler\CookieHandler

for this to work.

param $name [string] the name of the cookie param $invalidate [boolean] removeCookie($name, $invalidate)

Arguments

$arg0

string

$arg1

boolean

Response

\io\vertx\jphp\ext\web\Cookie

the cookie, if it existed, or null

Remove a headers end handler

removeHeadersEndHandler( $arg0) : boolean

Arguments

$arg0

integer

Response

boolean

true if the handler existed and was removed, false otherwise

request

request() : \io\vertx\jphp\core\http\HttpServerRequest

Response

\io\vertx\jphp\core\http\HttpServerRequest

the HTTP request object

Restarts the current router with a new path and reusing the original method. All path parameters are then parsed and available on the params list.

reroute( $arg0,  $arg1 = null) : void

param $path [string] the new http path. reroute($path)

Restarts the current router with a new method and path. All path parameters are then parsed and available on the params list.

param $method [string] the new http request param $path [string] the new http path. reroute($method, $path)

Arguments

$arg0

string

$arg1

string

response

response() : \io\vertx\jphp\core\http\HttpServerResponse

Response

\io\vertx\jphp\core\http\HttpServerResponse

the HTTP response object

Get the session. The context must have first been routed to a @see \io\vertx\jphp\ext\web\handler\SessionHandler for this to be populated.

session() : \io\vertx\jphp\ext\web\Session

Sessions live for a browser session, and are maintained by session cookies.

Response

\io\vertx\jphp\ext\web\Session

the session.

Set the acceptable content type. Used by

setAcceptableContentType( $arg0) : void

Arguments

$arg0

string

Set the body. Used by the @see \io\vertx\jphp\ext\web\handler\BodyHandler. You will not normally call this method.

setBody( $arg0) : void

Arguments

$arg0

Buffer

Set the session. Used by the @see \io\vertx\jphp\ext\web\handler\SessionHandler. You will not normally call this method.

setSession( $arg0) : void

Arguments

$arg0

Session

Set the user. Usually used by auth handlers to inject a User. You will not normally call this method.

setUser( $arg0) : void

Arguments

$arg0

User

If the context is being routed to failure handlers after a failure has been triggered by calling

statusCode() : integer
see \io\vertx\jphp\ext\web\RoutingContext::fail

then this will return that status code. It can be used by failure handlers to render a response, e.g. create a failure response page.

When the status code has not been set yet (it is undefined) its value will be -1.

Response

integer

the status code used when signalling failure

Get the authenticated user (if any). This will usually be injected by an auth handler if authentication if successful.

user() : \io\vertx\jphp\ext\auth\User

Response

\io\vertx\jphp\ext\auth\User

the user, or null if the current user is not authenticated.

vertx

vertx() : \io\vertx\jphp\core\Vertx

Response

\io\vertx\jphp\core\Vertx

the Vert.x instance associated to the initiating {@link Router} for this context