5
在symfony的1.4,你可以定義一個自定義路由類,在那裏你重寫URL的生成與定製邏輯,例如:自定義路由類
custom:
class: sfDoctrineRouteCollection
options:
model: Custom
prefix_path: /custom/category/:category_id
column: id
route_class: CustomDoctrineRoute
class CustomDoctrineRoute extends sfDoctrineRoute
{
public function generate($params, $context = array(), $absolute = false)
{
if (!isset($params['category_id'])) {
$params['category_id'] = sfContext::getInstance()->getRequest()->getParameter('category_id');
}
return parent::generate($params, $context, $absolute);
}
}
這樣寫url_for('custom_show', array('id'=> $object['id']))
並沒有理會上下文相關的參數(CATEGORY_ID)。
你如何處理這個是symfony2?
在這裏我已經通過覆蓋UrlGenerator解決了類似的問題:http://stackoverflow.com/questions/8877806/symfony2-use-object-to-set-route-parameters – Dziamid