starlite.controller#

class starlite.controller.Controller#

Bases: object

The Starlite Controller class.

Subclass this class to create ‘view’ like components and utilize OOP.

after_request: Optional[AfterRequestHookHandler]#

A sync or async function executed before a Request is passed to any route handler.

If this function returns a value, the request will not reach the route handler, and instead this value will be used.

after_response: Optional[AfterResponseHookHandler]#

A sync or async function called after the response has been awaited.

It receives the Request instance and should not return any values.

before_request: Optional[BeforeRequestHookHandler]#

A sync or async function called immediately before calling the route handler.

It receives the Request instance and any non-None return value is used for the response, bypassing the route handler.

cache_control: Optional[CacheControlHeader]#

A CacheControlHeader header to add to route handlers of this controller.

Can be overridden by route handlers.

dependencies: Optional[Dependencies]#

A string keyed dictionary of dependency Provider instances.

Type:

dependencies

etag: Optional[ETag]#

An etag header of type ETag to add to route handlers of this controller.

Can be overridden by route handlers.

exception_handlers: Optional[ExceptionHandlersMap]#

A dictionary that maps handler functions to status codes and/or exception types.

guards: Optional[List[Guard]]#

A list of Guard callables.

middleware: Optional[List[Middleware]]#

A list of Middleware.

opt: Optional[Dict[str, Any]]#

A string key dictionary of arbitrary values that can be accessed in Guards or wherever you have access to Request or ASGI Scope.

parameters: Optional[ParametersMap]#

A mapping of Parameter definitions available to all application paths.

response_class: Optional[ResponseType]#

A custom subclass of [starlite.response.Response] to be used as the default response for all route handlers under the controller.

response_cookies: Optional[ResponseCookies]#

A list of [Cookie](starlite.datastructures.Cookie] instances.

response_headers: Optional[ResponseHeadersMap]#

A string keyed dictionary mapping ResponseHeader instances.

tags: Optional[List[str]]#

A list of string tags that will be appended to the schema of all route handlers under the controller.

security: Optional[List[SecurityRequirement]]#

A list of dictionaries that to the schema of all route handlers under the controller.

type_encoders: Optional[TypeEncodersMap]#

A mapping of types to callables that transform them into types supported for serialization.

__init__(owner: Router) None#

Initialize a controller.

Should only be called by routers as part of controller registration.

Parameters:

owner – An instance of ‘Router’

path: str#

A path fragment for the controller.

All route handlers under the controller will have the fragment appended to them. If not set it defaults to ‘/’.

owner: Router#

The Router or Starlite app that owns the controller.

This value is set internally by Starlite and it should not be set when subclassing the controller.

get_route_handlers() List[BaseRouteHandler]#

Get a controller’s route handlers and set the controller as the handlers’ owner.

Returns:

A list containing a copy of the route handlers defined on the controller