Question

With a stable version of Zend Framework 2 on the horizon, we have been slowly implementing some ZF2 components, changing conventions, implementing namespaces, etc. It would be nice to have all of our controllers namespaced prior to the move, just to simply things, but I haven't found a good way to accomplish that goal.

Does anyone have any suggestions on controller namespacing in ZF1? I don't mind editing the ZF1 library files at this point.

namespace Product;

use Zend_Controller_Action as AbstractActionController;

class IndexController extends AbstractActionController
{}
Was it helpful?

Solution

Took a while, but I found that this can be done with a custom dispatcher:

<?php

namespace Webjawns\Controller\Dispatcher;

use Zend_Controller_Dispatcher_Standard as Dispatcher;

class Standard extends Dispatcher
{
    /**
     * Format action class name
     *
     * @param string $moduleName Name of the current module
     * @param string $className Name of the action class
     * @return string Formatted class name
     */
    public function formatClassName($moduleName, $className)
    {
        return $this->formatModuleName($moduleName) . '\Controller\\' . $className;
    }

    /**
     * Convert a class name to a filename
     *
     * @param string $class
     * @return string
     */
    public function classToFilename($class)
    {
        return str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
    }
}

Then somewhere in the entry point to the application:

Zend_Controller_Front::getInstance()->setDispatcher(
    new \Webjawns\Controller\Dispatcher\Standard());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top