Preparation

Let’s say we have a show method that we want to trigger using the route. We also have an intention to protect the route by VerifyUser middleware.

  1. Define a route (Configuration/Routes.yml)

    demo_photos-show:
      path:         api/demo/photos/{photo}
      controller:   LMS\Demo\Controller\PhotoApiController::show
      methods:      GET
      format:       json
      requirements:
        user:       \d+
        photo:      \d+
      defaults:
        plugin:     PhotoApi
        user:
        photo:
      options:
        middleware:
          - LMS\Routes\Middleware\Api\VerifyUser:user
    
  2. Register Plugin namespace (ext_localconf.php)

    \TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
        'LMS.demo',
        'PhotoApi',
        [
            'PhotoApi' => 'show'
        ],
        [
            'PhotoApi' => 'show'
        ]
    );
    

    Tip

    Plugin registration in Configuration/TCA/Overrides/tt_content.php is optional.

  3. Create photo table (ext_tables.sql)

    CREATE TABLE tx_demo_domain_model_photo (
      url varchar(128) DEFAULT '' NOT NULL
    );
    
  4. Create TCA (Configuration/TCA/tx_demo_domain_model_photo.php)

    <?php
        declare(strict_types = 1);
    
        return [
            'ctrl' => [
                'title' => 'Photo',
                'label' => 'url',
                'crdate' => 'crdate',
                'delete' => 'deleted',
                'searchFields' => 'url'
            ],
            'interface' => [
                'showRecordFieldList' => 'url'
            ],
            'types' => [
                '1' => [
                    'showitem' => '
                        url
                    '
                ]
            ],
            'columns' => [
                'url' => [
                    'exclude' => true,
                    'label' => 'Url',
                    'config' => [
                        'type' => 'input',
                        'eval' => 'trim'
                    ]
                ]
            ]
        ];
    
  5. Create model (Classes/Domain/Model/Photo.php)

    <?php
        declare(strict_types = 1);
    
        namespace LMS\Demo\Domain\Model;
    
        class Photo extends \LMS\Facade\Model\AbstractModel
        {
            /**
             * @var string
             */
            protected $url;
    
            /**
             * @param string $url
             */
            public function setUrl(string $url): void
            {
                $this->url = $url;
            }
    
            /**
             * @return string
             */
            public function getUrl(): string
            {
                return $this->url;
            }
        }
    
  6. Create Controller (Classes/Controller/PhotoApiController.php)

    <?php
        declare(strict_types = 1);
    
        namespace LMS\Demo\Controller;
    
        use LMS\Demo\Domain\Model\Photo;
    
        class PhotoApiController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
        {
            /**
            * @param \LMS\Demo\Domain\Model\Photo $photo
            * @return string
            */
            public function showAction(Photo $photo): string
            {
                return json_encode(
                    $photo->_getProperties()
                );
            }
        }
    

Tip

Of course, you can skip steps 3, 4, 5 if you are not dealing with models.