Gab*_*tos 7 php zend-framework2
我对Zend Framework 2有疑问:
我有图书馆/系统和图书馆/ Zend.系统是我的自定义库,我想配置它(路由,模块等,并将用户重定向到正确的模块,控制器和/或操作).
我不想在每个application/modules/ModuleName/Module.php文件中执行此操作.因此,我的库/系统可以完成与应用程序配置相关的所有操作.
如上面的评论中所述:注册bootstrap-event并在那里添加新路由:
<?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);
}
}
Run Code Online (Sandbox Code Playgroud)
$app = $e->getParam('application');确实返回了一个实例Zend\Mvc\Application.看看那里可以看到哪些附加部件可以到达那里.在bootstrap实际调度确实发生之前触发事件.
请注意,ZendFramework 1路由并不总是与ZendFramework 2路由兼容.
更新评论
public function initCustom($e)
{
$app = $e->getParam('application');
// Init a new router object and add your own routes only
$app->setRouter($newRouter);
}
Run Code Online (Sandbox Code Playgroud)
更新到新问题
<?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
}
}
Run Code Online (Sandbox Code Playgroud)
这只发生在一个 zf2模块中(命名Application也可以是唯一的模块).这不符合您的需求?你可以: