Search code examples
zend-frameworkzend-framework2

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


I recently started programming with Zend Framework.I want to change module name, controller name and action name of a module in my framework through coding, while coding in the same framework.

Its name is Application(module & controller) and I want to change it to Institute. Is this possible through coding?

I searched through Google for help, but either i couldn't find it or understand it properly. Any help would be appreciated.


Solution

  • This is really just a case of renaming things:

    Update all namespaces from Application to Institute in all the classes in the module including the Module.php

    Update the name of the controller and it's entry in config/module.config.php

    Make sure you update the name of your view directory if you have one in the module, ie view/application/index etc to view/institute/index and make sure you update the entry in module.config.php to the same path

    Update name of Application directory to Institute

    Update the name in the array of modules in modules.config.php or if you are using an earlier version application.config.php from under the modules key.

    That's all I can think of you would need to do

    ******** EDIT ********

    So the basic idea would be as follows:

    Add a console in a new module (I've used zend mvc console but you should probably use https://github.com/zfcampus/zf-console as mvc console is deprecated)

    module.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");
        }
    }
    

    This can be run like

    php public/index.php module rename Application Institute
    

    I've only done renaming the module here and renaming the namespace in the Module.php

    to finish this off you would need to recursively find all .php files in the Application directory and loop over applying the string replace (which should be improved really). Then you could update the view and application level config too with similar logic and using the steps above.

    The code I've written is pretty bad and probably insecure but might help you along the way