Here is an example about building dynamic route。
基本上是:
- 你添加一個偵聽事件
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);
無論如何,我仍然不會真的明白你想怎麼做...即使在你最後編輯。
來源
2013-01-25 10:39:38
j0k
你是什麼意思的網站實例? – j0k
爲了簡短起見,對於用戶可能是模塊新聞,anche行動articledetail,併爲其他用戶可能是模塊anothrnews和行動anotherarticledetail,取決於他們的憑據 – ilSavo