我正在尝试在层压中添加新的控制器和路由,我有一些像这样的模块结构:
ConfigProvider 包含:
<?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 被注入到 config.php 中
但是全都是 404
/api/resource/*
有什么线索我做错了吗?
根据您的配置,您似乎正在尝试在 MVC 应用程序中使用 Mezzio 配置。 Laminas MVC 需要一个 Module.php 文件,该文件应公开 getConfig 方法。
/** @return array<string, mixed> */
public function getConfig(): array
{
return include __DIR__ . '/../config/module.config.php';
}
此外, Mezzio 不使用控制器,它的 psr 7 / 15 中间件。所以我不确定你那里发生了什么。您需要选择一个框架。 Mezzio 和 Laminas MVC 不一样,在一种 MVC 中有效的方法在另一种中可能无效。