0

我已经开始了 Laminas 的最新教程。

名为 Provider 的新模块的路由不起作用

发生 404 错误页面未找到。请求的 URL 无法通过路由匹配。

  • 在查看我的 Module.php 代码时,我看到:

getConfig() 没有被调用,但是

getServiceConfig() 和 getControllerConfig() 是。

应用程序模块中的 getConfig 也不被调用

<?php

namespace Provider;

use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Db\ResultSet\ResultSet;
use Laminas\Db\TableGateway\TableGateway;

use Laminas\ModuleManager\Feature\AutoloaderProviderInterface;
use Laminas\ModuleManager\Feature\ConfigProviderInterface;


class Module implements ConfigProviderInterface, AutoloaderProviderInterface
{

    public function getConfig()
    {       

        die ("getConfig");

        return include __DIR__ . '/../config/module.config.php';
    }

    public function getAutoloaderConfig()
    {   

        //die ("getAutoloaderConfig");


        //return array(
        //      'Laminas\Loader\StandardAutoloader' => array(
        //              'namespaces' => array(
        //                      __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
        //              ),
        //      ),
        //);
    }


    public function getServiceConfig()
    {   

        //die ("getServiceConfig");


        return [
                'factories' => [
                        Model\ProviderTable::class => function($container) {
                        $tableGateway = $container->get(Provider\ProviderTableGateway::class);
                        return new Model\ProviderTable($tableGateway);
                    },
                    Model\ProviderTableGateway::class => function ($container) {
                        $dbAdapter = $container->get(AdapterInterface::class);
                        $resultSetPrototype = new ResultSet();
                        $resultSetPrototype->setArrayObjectPrototype(new Model\Album());
                        return new TableGateway('provider', $dbAdapter, null, $resultSetPrototype);
                    },
                    ],
                    ];
}


    public function getControllerConfig()
    {

        //die ("getControllerConfig");


        return [
            'factories' => [
                    Controller\ProviderController::class => function($container) {
                        return new Controller\ProviderController(
                                $container->get(Model\ProviderTable::class)
                                );
                    },
                    ],
                    ];
    }





}
4

3 回答 3

2

您需要启用开发模式。运行 composer development-enable到主动开发模式。

于 2020-06-25T03:51:18.327 回答
1

也许作曲家 json 没有更新(my-application/composer.json)

"autoload": {
    "psr-4": {
        "Application\\": "module/Application/src/",
        "Provider\\": "module/Provider/src/"
    }
},

更新自动加载类图:

composer dump-autoload

https://docs.laminas.dev/tutorials/getting-started/modules/#autoloading

于 2021-03-05T14:40:34.630 回答
0

有没有添加路由器配置?

在上面的附加代码中,您具有以下功能:

public function getConfig()
    {       

        //die ("getConfig");  // BE SURE YOU REMOVE THIS LINE

        return include __DIR__ . '/../config/module.config.php';
    }

它包含用于其他设置的文件。在这个文件“/../config/module.config.php”中,你应该添加你的路由器配置。它应该如下所示:

return [

//... 其他设置

    'router' => [
        'routes' => [
            // Literal route named "home"
            'home' => [
                'type' => 'literal',
                'options' => [
                    'route' => '/',
                    'defaults' => [
                        'controller' => 'Application\Controller\IndexController',
                        'action' => 'index',
                    ],
                ],
            ],
            // Literal route named "contact"
            'contact' => [
                'type' => 'literal',
                'options' => [
                    'route' => 'contact',
                    'defaults' => [
                        'controller' => 'Application\Controller\ContactController',
                        'action' => 'form',
                    ],
                ],
            ],
        ],
    ],
];

可以找到更多阅读https://docs.laminas.dev/laminas-router/routing/#simple-example-with-two-literal-routes

于 2020-06-03T18:20:56.593 回答