Preparation¶
Let’s say we have the store
method which creates a photo.
We also have an intention to protect the route by Throttle middleware.
Define a route (Configuration/Routes.yml)
demo_photos-store: path: api/demo/photos controller: Vendor\Demo\Controller\PhotoApiController::store methods: POST format: json requirements: url: defaults: plugin: PhotoApi url: options: middleware: - auth - LMS\Routes\Middleware\Api\Throttle:10,1
Register Plugin namespace (ext_localconf.php)
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin( 'LMS.demo', 'PhotoApi', [ 'PhotoApi' => 'store' ], [ 'PhotoApi' => 'store' ] );
Tip
Plugin registration in
Configuration/TCA/Overrides/tt_content.php
is optional.Create photo table (ext_tables.sql)
CREATE TABLE tx_demo_domain_model_photo ( url varchar(128) DEFAULT '' NOT NULL );
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' ] ] ] ];
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; } }
Create a repository (Classes/Domain/Repository/PhotoRepository.php)
<?php declare(strict_types = 1); namespace LMS\Demo\Domain\Repository; class PhotoRepository extends \LMS\Facade\Repository\AbstractRepository { // ... }
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 string $url * @return string */ public function storeAction(string $url): string { $created = (bool)Photo::create(compact('url')); return json_encode(compact('created'); } }
Tip
Steps 3, 4, 5, 6 are required only if you deal with a model.