【发布时间】:2015-01-14 23:05:20
【问题描述】:
我有一个自定义的Silex\RouteCollection 我想注册...
class RouteCollectionProvider extends RouteCollection
{
public function __construct() {
$this->add(
'Index',
new Route('/', array(
'method' => 'get',
'controller' => 'index',
'action' => 'index'
)
));
}
}
...在引导过程中:
$app = new Silex\Application();
/** here **/
$app->run();
我可以使用:
$app = new Silex\Application();
$routes = new RouteCollectionProvider();
foreach ($routes->getIterator() as $route) {
$defaults = $route->getDefaults();
$pattern = $route->getPath();
$callback = 'Controller\\'
. ucfirst($defaults['controller'])
. 'Controller::'
. $defaults['action']
. 'Action';
$app->get($pattern, $callback);
}
$app->run();
我不喜欢在那里初始化这些路由。 你知道 Silex 有哪个地方更适合这个吗?
我不能使用$app->register(),因为它被调用太晚了,并且路由不会在其中激活。
也许有一个我可以使用的事件
$app->on('beforeCompileRoutesOrSomething', function() use ($app) {
// initialize routes
}
还是 Dispatcher 中的钩子?
我的目标是不要在那里收集大量$app->get() 或$app->post()。我也知道我可以 ->mount() 和 controller 但我的所有 get 定义仍然在我的引导程序中,而不是在提供程序中。
【问题讨论】: