【问题标题】:Using a RouteCollectionProvider in Silex在 Silex 中使用 RouteCollectionProvider
【发布时间】: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 定义仍然在我的引导程序中,而不是在提供程序中。

【问题讨论】:

    标签: php events silex dispatch


    【解决方案1】:

    这个帖子解决了这个问题:Scaling Silex pt. 2

    $app = new Application;
    
    $app->extend('routes', function (RouteCollection $routes, Application $app) {
        $routes->addCollection(new MyCustomRouteCollection);
         return $routes;
    });
    
    $app->run();
    

    【讨论】:

      猜你喜欢
      • 2013-04-05
      • 2014-09-15
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      相关资源
      最近更新 更多