これは、動的ルートの構築に関する例です。
基本的:
- イベントにリスナーを追加します
routing.load_configuration
- このリスナーは、データベースからルートを取得し、それらを現在のルーティング キャッシュの先頭に追加します
ここにきれいなスニペットがあります:
<?php
class frontendConfiguration extends sfApplicationConfiguration
{
public function configure()
{
$this->dispatcher->connect('routing.load_configuration', array($this, 'listenToRoutingLoadConfigurationEvent'));
}
public function listenToRoutingLoadConfigurationEvent(sfEvent $event)
{
$routing = $event->getSubject();
$products = Doctrine::getTable('Product')->findAll();
foreach ($products as $product)
{
if (0 == strlen($product->route))
{
continue;
}
$name = 'product_'.$product->id;
$route = new sfRoute(
$product->route,
array('module' => 'browse', 'action' => 'catalog', 'product' => $product->id),
array('product' => '\d+'),
array('extra_parameters_as_query_string' => false)
);
$routing->prependRoute($name, $route);
}
}
}
編集:
コンテキストを使用して、アクションからルーティングを取得できます。
$this->getContext()->getRouting()
したがって、アクションからルートを追加する場合は、次のようにします。
$route = new sfRoute(
'/my-route',
array('module' => 'browse', 'action' => 'catalog', 'product' => 456),
array('product' => '\d+'),
array('extra_parameters_as_query_string' => false)
);
$this->getContext()->getRouting()->prependRoute('my-route', $route);
とにかく、私はまだあなたがそれをどのように作りたいのか本当に理解していません.最後の編集の後でも.