[英]How to add custom routes to Symfony 2 before container compilation?
Let me explain the case in further details because the title doesn't explain it very well without being too long. 让我更详细地说明该情况,因为标题过长而无法很好地说明该情况。
I'm building a mini framework on top of SF2 to use with our legacy system. 我正在SF2之上构建一个微型框架,以用于我们的旧系统。 We developed a mini, simple plugin system, each plugin will have its own routing.xml which may look like this:
我们开发了一个迷你的简单插件系统,每个插件都有自己的routing.xml,如下所示:
routes:
admin_plugins:
pattern: /manager/
defaults: { _controller: plugins\riCore\AdminController::indexAction }
We want to obviously add these routes into the routeCollection, but there is 1 tweak: we want to prepend the route pattern and id with the plugin name. 我们显然想将这些路由添加到routeCollection中,但有1个调整:我们想在路由模式和ID前面加上插件名称。 We used to loop through the list of plugin and do it manually like this:
我们曾经循环浏览插件列表,并手动执行以下操作:
self::$container->get('router')->getRouteCollection()->add($plugin_lc_name . '_' . $key, new Route($route['pattern'], $route['defaults'], $route['requirements'], $route['options']));
However, now that we moved to use SF2 kernel and do a compilation of the container, we began to run into this problem: 但是,现在我们开始使用SF2内核并进行了容器的编译,我们开始遇到这个问题:
[21-Oct-2012 08:40:57] PHP Fatal error: Call to undefined method Symfony\Component\Routing\RouteCollection::__set_state() in plugins\cache\prod\pluginsProdProjectContainer.php on line 791
So I figured it could be due to the fact that we tried to getRouteCollection() the too soon and perhaps there must be a way around? 因此,我认为这可能是由于我们过早尝试获取getRouteCollection()的事实,也许必须有一种解决方法?
To answer my question, yes it seems to work. 要回答我的问题,是的,它似乎有效。 I just haven't found a way to make SF2 cache those routes yet.
我只是还没有找到一种使SF2缓存那些路由的方法。 Below is part of the code I'm using to add dynamic routes.
以下是我用来添加动态路由的部分代码。 As you can see I want to add certain prefixes to routes
如您所见,我想向路由添加某些前缀
$plugin_lc_name = strtolower($plugin);
foreach ($plugins_settings[$plugin]['routes'] as $key => $route) {
$route = array_merge(array('pattern' => '',
'defaults' => array(),
'requirements' => array(),
'options' => array()), $route);
if (strpos($route['pattern'], '/') !== false)
$route['pattern'] = $plugin_lc_name . $route['pattern'];
else
$route['pattern'] = $plugin_lc_name . '_' . $route['pattern'];
$container::get('router')->getRouteCollection()->add($plugin_lc_name . '_' . $key, new Route($route['pattern'], $route['defaults'], $route['requirements'], $route['options']));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.