docs/tutorials/add-route.md
This document is part of the Appwrite contributors' guide. Before you continue reading this document make sure you have read the Code of Conduct and the Contributing Guide.
Setting an alias allows the route to be also accessible from the alias URL. The first parameter specifies the alias URL, the second parameter specifies default values for route parameters.
App::post('/v1/storage/buckets/:bucketId/files')
->alias('/v1/storage/files', ['bucketId' => 'default'])
Used as an abstract description of the route.
App::post('/v1/storage/buckets/:bucketId/files')
->desc('Create File')
Groups array is used to group one or more routes with one or more hooks functionality.
App::post('/v1/storage/buckets/:bucketId/files')
->groups(['api'])
In the above example groups() is used to define the current route as part of the routes that shares a common init middleware hook.
App::init()
->groups(['api'])
->action(
some code.....
);
Labels are very straightforward and easy to use and understand, but at the same time are very robust. Labels are passed from the controllers route and used to pick up key-value pairs to be handled in a centralized place along the road. Labels can be used to pass a pattern in order to be replaced on the other end. Appwrite uses different labels to achieve different things, for example:
App::post('/v1/storage/buckets/:bucketId/files')
->label('scope', 'files.write')
App::post('/v1/account/create')
->label('audits.event', 'account.create')
->label('audits.resource', 'user/{response.$id}')
->label('audits.userId', '{response.$id}')
App::post('/v1/account/jwt')
->label('sdk', new Method(
namespace: 'account',
name: 'createJWT',
description: '/docs/references/account/create-jwt.md',
auth: [],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_CREATED,
model: Response::MODEL_JWT,
)
],
responseType: ResponseType::JSON,
))
App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview')
->label('cache', true)
->label('cache.resource', 'file/{request.fileId}')
When using the example below, we configure the abuse mechanism to allow this key combination constructed from the combination of the ip, http method, url, userId to hit the route maximum 60 times in 1 hour (60 seconds * 60 minutes).
App::post('/v1/storage/buckets/:bucketId/files')
->label('abuse-key', 'ip:{ip},method:{method},url:{url},userId:{userId}')
->label('abuse-limit', 60)
->label('abuse-time', 3600)
[] are parsed and replaced with their real values.App::post('/v1/storage/buckets/:bucketId/files')
->label('event', 'buckets.[bucketId].files.[fileId].create')
As the name implies, param() is used to define a request parameter.
param() accepts 6 parameters :
App::get('/v1/account/logs')
->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true)
inject is used to inject dependencies pre-bounded to the app.
App::post('/v1/storage/buckets/:bucketId/files')
->inject('user')
In the example above, the user object is injected into the route pre-bounded using App::setResource().
App::setResource('user', function() {
some code...
});
Action populates the actual route code and has to be very clear and understandable. A good route stays simple and doesn't contain complex logic. An action is where we describe our business needs in code, and combine different libraries to work together and tell our story.
App::post('/v1/account/sessions/anonymous')
->action(function (Request $request) {
some code...
});