Preparation

  1. Define appropriate route.

    Create your route collection file under Configuration/Routes.yml:

  2. Your Resource repository must extend our predefined AbstractRepository.

    Create your repository under Classes/Domain/Repository/EntityRepository.php:

    <?php
       declare(strict_types = 1);
    
       namespace Vendor\Demo\Domain\Repository;
    
       class EntityRepository extends \LMS\Facade\Repository\AbstractRepository
       {
          // ...
       }
    

    Tip

    EntityRepository replace with your real repository name.

  3. Create your own JsonView.

    Create your view under Classes/Mvc/View/JsonView.php:

    <?php
       declare(strict_types = 1);
    
       namespace Vendor\Demo\Mvc\View;
    
       class JsonView extends \LMS\Facade\Mvc\View\JsonView
       {
           /**
            * @var array
            */
           protected $configuration = [
               'entity' => [
                   '_descendAll' => [
                       '_exclude' => ['pid']
                   ]
               ]
           ];
       }
    

    Tip

    getRootName should return the name of your resource. In this case it’s entity Don’t forget to rename it with your real name.

  4. Your Controller must extend our predefined AbstractApiController.

    Create your view under Classes/Controller/DemoApiController.php:

    <?php
       declare(strict_types = 1);
    
       namespace Vendor\Demo\Controller;
    
       class DemoApiController extends \LMS\Facade\Controller\AbstractApiController
       {
          /**
            * @var string
            */
           public $defaultViewObjectName = \Vendor\Demo\Mvc\View\JsonView::class;
    
          /**
            * {@inheritdoc}
            */
           protected function getRootName(): string
           {
               return 'entity';
           }
    
           /**
             * {@inheritdoc}
             */
            protected function getResourceRepository(): RepositoryInterface
            {
                return EntityRepository::make();
            }
       }
    
    1. AbstractApiController Predefined controller that implements all needed CRUD actions. It also uses JSON view instead of standard one, that’s pretty much what you want when dealing with API.
    2. getResourceRepository ApiController needs to know which resource it’s connected to. So you need to return the actual Resource repository. Before, you have already extended your fresh repository from our predefined AbstractRepository so make() is available for making a fresh instance of it.
    3. getRootName Connected to Response that you get. This is actually the root name of your response data. We could use the default one, but current approach gives you a possibility to manipulate data using JsonView

    Tip

    defaultViewObjectName is connected to your JsonView.

CRUD

  1. Represents the route name. It should be unique. Name could be used in MakeSlugViewHelper on demand.
  2. :yaml:`path` is the route you expect to see as an API endpoint. Later you need to use this url to perform the request.
  3. :yaml:`controller` Indicates which Extbase Controller and action inside it should be triggered during the request.
  4. :yaml:`methods` Specifies which request type is granted to call the action ( could be more than just one).
  5. :yaml:`schemes` Which protocol should be used during the request.
  6. :yaml:`format` Indicates which view format we deal with. By default Extbase will trigger MyView.html, if it is set to json, the result will be MyView.json
  7. :yaml:`defaults` This is a container that may contain predefined values for your action, so-called arguments.
  8. :yaml:`plugin` Since v10 is required and represents the plugin name of the needed extension::action itself.
  9. :yaml:`data` Required when we pass any data using POST or PUT methods. Just keep it empty for those methods.
  10. :yaml:`requirements` Allow us to control the variable type for dynamic variable in the route.
  11. :yaml:`middleware` Allow us to call certain list of middleware for the route.
extension_controller-action:
   path:         api/path/to/my/route/{entity}
   controller:   Vendor\Demo\Controller\DemoApiController::myActionName
   methods:      [GET, POST, PUT, DELETE]
   schemes:      [https, http]
   format:       json
   defaults:
      plugin:    Pi1
      data:
   requirements:
      entity:    \d+
   options:
      middleware:
         - auth
         - LMS\Routes\Middleware\Api\VerifyAdminBackendSession
         - Vendor\Demo\Middleware\Api\MyMiddleware