Search code examples
typo3middlewareextbasetypo3-extensions

Handle TYPO3 extbase controller action with middleware and URL pattern


I need some simple solution with TYPO3 middleware which identify URL pattern and return json response accordingly.

URL pattern: https://example.com/[api]/[ext_name]/[controllerName]/[actionName]

Eg:https://example.com/api/event/eventManagement/list

Based on this URL, it should call controller action. I following document example for writting middleware but it throws error.

enter image description here

Here below snippet also having the same error.

Here I registered middleware

<?php

return [
    'frontend' => [
        'picsoholic-rest' => [
            'target' => \Vendor\Ext\Middleware\RestMiddleware::class,
            'description' => 'Attempts to resolve the Extbase controller action',
            'after' => [
                'typo3/cms-frontend/prepare-tsfe-rendering'
            ]
        ],
    ],
];

services.yaml

services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

  Vendor\Ext\:
    resource: '../Classes/*'
    exclude: '../Classes/Domain/Model/*'

  Vendor\Ext\Middleware\RestMiddleware:
    public: true

and middleware class

<?php

namespace Devzspace\PicsoholicCore\Middleware;

use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class RestMiddleware implements MiddlewareInterface
{
    /** @var ResponseFactory */
    private $responseFactory;

    /** @var RequestFactory */
    private $requestFactory;

    /** @var ClientInterface */
    private $client;

    public function __construct(
        ResponseFactoryInterface $responseFactory,
        RequestFactoryInterface $requestFactory,
        ClientInterface $client
    ) {
        $this->responseFactory = $responseFactory;
        $this->requestFactory = $requestFactory;
        $this->client = $client;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        // Check if request is for api.
        if ($request->getRequestTarget() === '/api') {
            // create logic for calling controller action for json response
        }
        return $handler->handle($request);
    }    
}

I want to handle GET, POST, DELETE, PUT request with the middleware. Any hint for creating it without third party extension?

Some solution to handle Extbase controller action with middleware rest URL


Solution

  • I recommend to read the following section of the official docs: https://docs.typo3.org/m/typo3/reference-coreapi/12.4/en-us/ApiOverview/DependencyInjection/Index.html#public

    Pasted here for completeness:

    Direct access includes instantiation via GeneralUtility::makeInstance() with constructor arguments.

    Also the next section mentions your error:

    https://docs.typo3.org/m/typo3/reference-coreapi/12.4/en-us/ApiOverview/DependencyInjection/Index.html#errors-resulting-from-wrong-configuration

    If objects that use dependency injection are not configured properly, one or more of the following issues may result. In such a case, check whether the class has to be configured as public: true.

    ArgumentCountError is raised on missing dependency injection for Constructor injection:

    I bet your middleware is not marked as public within DI, and marking it as public will solve the issue? Maybe you can also add more context from the stack trace? That should reveal how the instance is created.