在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);
}
}
Run Code Online (Sandbox Code Playgroud)
这允许编写url_for('custom_show', array('id'=> $object['id']))而不是打扰上下文相关参数(category_id).
你如何处理这个是symfony2?
我可以想到两种方法。第一个也是最简单的方法是用您自己的类扩展 Router 类,并告诉 symfony 在您的parameters.yml 或 config.yml 中使用您的类:
parameters:
router.class: Company\CoreBundle\Routing\MyCustomRouter
Run Code Online (Sandbox Code Playgroud)
有一个更强大(读:复杂)的解决方案,它允许您通过覆盖或扩展整个路由器服务来定义对路由器类的更多依赖项。有一个名为BeSimpleI18nRoutingBundle的捆绑包可以执行此操作,您可以查看它以了解它是如何完成的。
具体来说,请注意CompilerPassrouter ,他们用自己的服务替换了默认服务。然后,您必须在自己的路由器类中实现RouterInterface 。在这个特定的包中,他们注入原始的默认路由器(在编译器通道中重命名之后)。
| 归档时间: |
|
| 查看次数: |
931 次 |
| 最近记录: |