【问题标题】:Laminas Config Module Routing层配置模块路由
【发布时间】:2020-04-27 14:20:10
【问题描述】:

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

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

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

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

getConfig() 没有被调用,但是

getServiceConfig() 和 getControllerConfig() 是。

Application模块中的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)
                                );
                    },
                    ],
                    ];
    }





}

【问题讨论】:

  • 我自己不熟悉 Laminas MVC。您更有可能在 Laminas 官方论坛 (discourse.laminas.dev) 上得到回复。

标签: module laminas


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    也许 composer 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

    【讨论】:

      【解决方案3】:

      你添加了路由器配置吗?

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

      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

      【讨论】:

      • 路由器配置在:return include DIR。 '/../config/module.config.php';但是这条线永远不会被调用,因为 getConfig() 没有被调用。
      猜你喜欢
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      • 2016-12-28
      • 2020-06-08
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      相关资源
      最近更新 更多