Laminas routing for controller route not working

I’m trying to add new controller and routes for it in laminas, I have some stracture of module like this: https://i.stack.imgur.com/tOts6.png

ConfigProvider contains:

<?php

declare(strict_types=1);

namespace RoleApi;


use Laminas\Router\Http\Segment;
use Laminas\ServiceManager\Factory\InvokableFactory;
use RoleApi\Application\Controller\ResourceController;

/**
 * The configuration provider for the RoleApi module
 *
 * @see https://docs.laminas.dev/laminas-component-installer/
 */
class ConfigProvider
{
    /**
     * Returns the configuration array
     *
     * To add a bit of a structure, each section is defined in a separate
     * method which returns an array with its configuration.
     */
    public function __invoke(): array
    {
        return [
            'dependencies' => $this->getDependencies(),
            'templates' => $this->getTemplates(),
            'controllers' => $this->getControllers(),
            'router' => $this->getRouter(),
        ];
    }

    /**
     * Returns the container dependencies
     */
    public function getDependencies(): array
    {
        return [
            'invokables' => [
            ],
            'factories' => [
            ],
        ];
    }

    /**
     * Returns the templates configuration
     */
    public function getTemplates(): array
    {
        return [
            'paths' => [
            ],
        ];
    }

    public function getControllers(): array
    {
        return [
            'factories' => [
                ResourceController::class => InvokableFactory::class,
            ],
        ];
    }

    public function getRouter(): array
    {
        return [
            'routes' => [
                'resource' => [
                    'type' => Segment::class,
                    'options' => [
                        'route' => '/api/resource[/:action[/:id]]',
                        'constraints' => [
                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'id' => '[0-9]+',
                        ],
                        'defaults' => [
                            'controller' => ResourceController::class,
                            'action' => 'index',
                        ],
                    ],
                ],
            ]
        ];
    }
}

RoleApi\ConfigProvider::class is injected in config.php

But there’s a 404 on all /api/resource/*

Only routes that are described in routes.php are working:

<?php

declare(strict_types=1);

use Mezzio\Application;
use Mezzio\MiddlewareFactory;
use Psr\Container\ContainerInterface;

return static function (Application $app, MiddlewareFactory $factory, ContainerInterface $container): void {
    $app->get('/', App\Handler\HomePageHandler::class, 'home');
    $app->get('/ping', App\Handler\PingHandler::class, 'ping');
};

Any clues what I’m doing wrong?

You mix here Mezzio and laminas-mvc. You can not use the controller of laminas-mvc in a Mezzio application.

You are saying that basically mezzio have only handlers to deal with?

A request handler can handle different actions but this is not the recommended way. The single-responsibility principle (SRP) is good, easier to implement and to test.

The old documentation of Mezzio contains an example to handle multiple routes in a single class but this was removed for the above reasons.

2 Likes