Laminas routing for controller route not working

111 Views Asked by At

I'm trying to add new controller and routes for it in laminas, I have some stracture of module like this:enter image description here

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::clas is injected in config.php

But theres a 404 on all /api/resource/*

Any clues what I'm doing wrong?

1

There are 1 best solutions below

0
Tyrsson On

Just going based on your config it appears that you are trying to use Mezzio configuration in a MVC application. Laminas MVC expects a Module.php file which should expose a getConfig method.

    /** @return array<string, mixed> */
public function getConfig(): array
{
    return include __DIR__ . '/../config/module.config.php';
}

Furthermore, Mezzio does not use Controllers, its psr 7 / 15 middleware. So I am not sure what you have going on there. You need to pick a framework. Mezzio and Laminas MVC are not the same, what will work in one may not work in the other.