Controllers
Controllers
Angel3 has built-in support for controllers. This is yet another way to define routes in a manageable group, and can be leveraged to structure your application in the MVC format. You can also use the group()
method of any Router
.
The metadata on controller classes is processed via reflection only once, at startup. Do not believe that your controllers will be crippled by reflection during request handling, because that possibility is eliminated by pre-injecting dependencies.
Rather than extending from Routable
, controllers act as plugins when called. This pseudo-plugin will wire all your routes for you.
@Expose()
The glue that holds it all together is the Expose
annotation:
Allowing Null Values
Most fields are self-explanatory, save for as
and allowNull
. See, request parameters are mapped to function parameters on each handler. If a parameter is null
, an error will be thrown. To prevent this, you can pass its name to allowNull
.
Named Controllers and Actions
The other is as
. This allows you to specify a custom name for a controller class or action. ResponseContext
contains a method, redirectToAction
that can redirect to a controller action.
If you do not specify an as
, then controllers and actions will be available by their names in code. Reflection is cool, huh?
Interacting with Requests and Responses
Controllers can also interact with requests and responses. All you have to do is declare a RequestContext
or ResponseContext
as a parameter, and it will be passed to the function.
Transforming Data
You can use middleware to de/serialize data to be processed in a controller method.
Next Up
How to handle parse request bodies with Angel3
Last updated