I have a questiom regarding the Zend Framework 2:
I have library/System and library/Zend. the system is my custom library, which I want to configure de aplication (routes, modules, etc., and redirect user to correct module, controller and/or action).
I don't want to do this inside each application/modules/ModuleName/Module.php file. So, my library/System can do everything related to application configuration.
As said in the comments above: register to the bootstrap-event and add new routes there:
<?php
namespace Application;
use Zend\Module\Manager,
    Zend\EventManager\StaticEventManager;
class Module
{
    public function init(Manager $moduleManager)
    {
        $events = StaticEventManager::getInstance();
        $events->attach('bootstrap', 'bootstrap', array($this, 'initCustom'), 100);
    }
    public function initCustom($e)
    {
        $app = $e->getParam('application');
        $r = \Zend\Mvc\Router\Http\Segment::factory(array(
                'route'    => '/test',
                'defaults' => array(
                    'controller' => 'test'
                )
            )
        );
        $app->getRouter()->addRoute('test',$r);
    }
}
$app = $e->getParam('application'); does return an instance of Zend\Mvc\Application. Have a look there to see which additional parts you can get there. The bootstrap event is fired before the actual dispatching does happen.
Note that the ZendFramework 1 routes are not always compatible to the ZendFramework 2 ones.
Update to comments
public function initCustom($e)
{
    $app = $e->getParam('application');
    // Init a new router object and add your own routes only
    $app->setRouter($newRouter);
}
Update to new question
<?php
namespace Application;
use Zend\Module\Manager,
    Zend\EventManager\StaticEventManager;
class Module
{
    public function init(Manager $moduleManager)
    {
        $events = StaticEventManager::getInstance();
        $events->attach('bootstrap', 'bootstrap', array($this, 'initCustom'), 100);
    }
    public function initCustom($e)
    {
        $zendApplication = $e->getParam('application');
        $customApplication = new System\Application();
        $customApplication->initRoutes($zendApplication->getRouter());
        // ... other init stuff of your custom application
    }
}
This only happens in one zf2 module (named Application which can be the only one as well). This doesn't fit your needs? You could:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With