0

错误是:

发生 404 错误页面未找到。

请求的 URL 无法通过路由匹配。没有可用的例外

我已经用 Composer 注册了我的模块和命名空间,然后运行 ​​Composer dump-autoload。我的代码如下:

模块\相册\配置\module.config.php

<?php

declare(strict_types=1);

namespace Album;

use Laminas\Router\Http\Segment;
use Laminas\ServiceManager\Factory\InvokableFactory;

return [
    'router' => [
        'routes' => [
            'album' => [
                'type' => Segment::class,
                'options' => [
                    'route' => '/album[/:action[/:id]]',
                    'constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id' => '[0-9]+',
                    ],
                    'defaults' => [
                        'controller' => Controller\AlbumController::class,
                        'action' => 'index',
                    ],
                ],
            ],
        ],
    ],
    'controllers' => [
        'factories' => [
            Controller\AlbumController::class => InvokableFactory::class,
        ],
    ],
    'view_manager' => [
        'template_map' => [
            'album/index' => __DIR__ . '/../view/album/album/index.phtml',
        ],
        'template_path_stack' => [
            'album' => __DIR__ . '/../view',
        ],
    ],
];

模块\相册\src\Module.php

<?php

declare(strict_types=1);

namespace Album;

class Module
{
    public function getConfig() : array
    {
        return include __DIR__ . '/../config/module.config.php';
    }
}

模块\Album\src\Controller\AlbumController.php

<?php

declare(strict_types=1);

namespace Album\Controller;

use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;

class AlbumController extends AbstractActionController
{
    public function indexAction()
    {
        return new ViewModel();
    }
}

模块\相册\视图\相册\相册\index.phtml

Index page displays here...
4

1 回答 1

0

正如您将根指定为

'route' => '/album[/:action[/:id]]',

该 url 应该看起来像http://laminas.com/album,它将路由到 indexAction ,因为您已将该操作定义为默认值,但http://laminas.com/album/index将具有相同的效果。

于 2021-05-30T01:18:53.110 回答