1

我有这个路由器列表:

$router[] = $adminRouter = new RouteList('Admin');
$adminRouter[] = new Route('admin/<presenter>/<action>', 'Dashboard:default');

$router[] = $frontRouter = new RouteList('Front');
$frontRouter[] = new Route('<presenter>/<action>[/<id>]', 'Homepage:default');

但最好将这两个路由器列表分成模块文件夹。将来我可能会创建更多具有不同路由器列表的模块。如何将其分开,之后如何注册?

4

1 回答 1

3

使用路由工厂

在 config.neon

services:
  routeFactory.Front: FrontModule\RouteFactory
  routeFactory.Admin: AdminModule\RouteFactory
  route:
    class: \RouteFactory
    setup: 
      - addRouteFactory(@routeFactory.Front)
      - addRouteFactory(@routeFactory.Admin)

类RouteFactory:

class RouteFactory
{
  /** @var array */
  private $routerFactories = array();


  public function addRouteFactory(IRouteFactory $routeFactory)
  {
    $this->routeFactories[] = $routeFactory;
  }

  public function createRouter()
  {
    $router = new RouteList();
    foreach ($this->routeFactories as $routeFactory) {
      $router[] = $routeFactory->createRouter();
    }

    return $router;
  }
}

接口 IRouteFactory:

interface IRouteFactory
{
  public function createRouter();
}

模块路由工厂

namespace FrontModule;

class RouteFactory implements \IRouteFactory
{
  public function createRouter()
  {
    $router = new RouteList('Front');
    // your routes

   return $router;
  }
}
于 2014-07-18T04:07:01.043 回答