在 Zend 框架中设置模块名称、控制器名称和操作名称?

Set Module name, Controller name, and Action name in Zend framework?

我最近开始使用 Zend 编程 Framework.I 想更改 模块名称控制器名称通过编码在我的框架中的一个模块的动作名称,同时在同一框架中编码。

它的名字是Application(module & controller),我想把它改成Institute。这可以通过编码实现吗?

我在 Google 中搜索了帮助,但要么找不到,要么无法正确理解。任何帮助将不胜感激。

这真的只是一个重命名的例子:

更新模块中所有 类 中从 Application 到 Institute 的所有命名空间,包括 Module.php

更新控制器的名称及其在 config/module 中的条目。config.php

如果模块中有视图目录,请确保更新视图目录的名称,即 view/application/index 等到 view/institute/index,并确保更新模块中的条目。config.php 到相同的路径

将 Application 目录的名称更新为 Institute

更新模块中模块数组中的名称。config.php 或者如果您使用的是较早版本的应用程序。config.php 从模块键下。

这就是我能想到的您需要做的所有事情

******** 编辑 ********

所以基本思路如下:

在新模块中添加一个控制台(我使用过 zend mvc 控制台,但您可能应该使用 https://github.com/zfcampus/zf-console,因为不推荐使用 mvc 控制台)

模块。config.php

<?php
namespace Rename;

use Zend\ServiceManager\Factory\InvokableFactory;

return [
  'console' => array(
      'router' => array(
          'routes' => array(
              'rename-module' => array(
                    'options' => array(
                        'route'    => 'module rename <moduleNameOld> <moduleNameNew>',
                        'defaults' => array(
                            'controller' => Controller\IndexController::class,
                            'action'     => 'rename'
                        )
                    )
              )
          )
      )
  ),
  'controllers' => [
      'factories' => [
          Controller\IndexController::class => InvokableFactory::class,
      ],
  ],
];

IndexController.php

<?php
namespace Rename\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Console\Request as ConsoleRequest;
use Zend\Mvc\Console\Controller\AbstractConsoleController;

class IndexController extends AbstractConsoleController
{
    public function renameAction()
    {
        $request = $this->getRequest();

        // Make sure that we are running in a console and the user has not tricked our
        // application into running this action from a public web server.
        if (!$request instanceof ConsoleRequest) {
            throw new \RuntimeException('You can only use this action from a console!');
        }

        $moduleNameOld  = $request->getParam('moduleNameOld');
        $moduleNameNew  = $request->getParam('moduleNameNew');

        $module = file_get_contents(getcwd() . "/module/$moduleNameOld/src/Module.php", 'r+');
        $updatedModule = str_replace($moduleNameOld, $moduleNameNew, $module);
        file_put_contents(getcwd() . "/module/$moduleNameOld/src/Module.php", $updatedModule);

        rename("module/$moduleNameOld", "module/$moduleNameNew");
    }
}

这可以是 运行 就像

php public/index.php module rename Application Institute

我只在此处重命名了模块并在 Module.php

中重命名了命名空间

要完成此操作,您需要递归地查找 Application 目录中的所有 .php 文件并循环应用字符串替换(这确实应该改进)。然后您也可以使用类似的逻辑并使用上述步骤更新视图和应用程序级别的配置。

我写的代码很糟糕,可能不安全,但可能会对你有所帮助