@Api\Endpoint

Mark a class as endpoint for the TYPO3 RestAPi

There are two basic ways to register a Class as endpoint so the TYPO3 Restful API will route requests to it:

  • by using the nnrest::Endpoint()->register() method in the ext_localconf.php of your extension. This is useful to register all Classes in a certain namespace, e.g. My\Extension\Api\*.
  • By using the @Api\Endpoint() Annotation in the DocComment of the individual Class as described in this chapter.

Tip

Only use one of both!

Note, that by registering the Class as an Endpoint using the @Api\Endpoint() annotation, there is no need to use \nn\rest::Endpoint()->register() in the ext_localconf.php anymore – and vice versa. The nnrestapi extension will automatically traverse through all classes of the extension folder and find classes with this annotation in the DocComment.

Marking individual Classes as Endpoint

In the following example we will mark the class Example as an TYPO3 RestApi Endpoint by setting the @Api\Endpoint() Annotation in the comment block above the class.

Requests sent to https://www.yourwebsite.com/api/example/... will automatically be routed to this class.

Here is a full example

<?php

namespace My\Extension\Api;

use Nng\Nnrestapi\Annotations as Api;
use Nng\Nnrestapi\Api\AbstractApi;

/**
 * @Api\Endpoint()
 */
class Example extends AbstractApi
{
   // Your methods
}

The above endpoint can be reached over the URL:

https://www.mywebsite.com/api/example/...

Override the path segment / class name

By default, the first path segment after api/.../ is identical with the class name of your endpoint. If your class is named Example, then you can route the requests to it by calling the URL api/example/.

This can be overridden by setting a value in @Api\Endpoint("name").

In the following example, we would like to route requests to api/apples/... to the class Oranges. This can be achieved by setting @Api\Endpoint("apples"):

<?php

namespace My\Extension\Api;

use Nng\Nnrestapi\Annotations as Api;
use Nng\Nnrestapi\Api\AbstractApi;

/**
 * @Api\Endpoint("apples")
 */
class Oranges extends AbstractApi
{
   // Your methods
}

The above endpoint can be reached over the URL:

https://www.mywebsite.com/api/apples/...