【问题标题】:Auto Routing Bundle - and parameters? (Symfony CMF)自动路由捆绑包 - 和参数? (Symfony CMF)
【发布时间】:2014-07-04 14:50:29
【问题描述】:

我成功地让 Symfony CMF 和 Auto Routing Bundle(以及自定义控制器)以下列方式(路径)加载文档:

.../category/article-name

这很好用。但我需要这样的东西,可以通过标准 Symfony2 轻松实现:

.../category/article-name/{page}

其中页面作为参数传递给控制器​​, 例如。 $page = 2 -> 控制器处理文章文档的内容以仅显示给定页面 或 $page = null -> (所以根本没有 {page} 参数) - 如上所述,但显示的是页面默认值,即 1

如何制作 Symfony CMF 的 routing-auto 捆绑包以在路由中使用参数?

因此基本路径与此自动路由捆绑配置中的配置完全相同,但另外我可以使用传递给控制器​​的参数,以便可以对它们做出额外的决定。有什么提示吗?谢谢!

我的自动路由配置:

    MyApp\MyCmsBundle\Document\Article:
        content_path:
            #fixed path of all categories and therefore articles
            articles_categories_path:
                provider: [specified, { path: /cms/routes }]
                exists_action: use
                not_exists_action: throw_exception
            #category path
            category_path:
                provider: [content_method, { method: getRouteNodeCategory }]
                exists_action: use
                not_exists_action: throw_exception
        #article name
        content_name:
            provider: [content_method, { method: getTitle }]
            exists_action: [auto_increment, { pattern: -%d }]
            not_exists_action: create
    MyApp\MyCmsBundle\Document\ArticlesCategory:
        content_path:
            #fixed path of all categories and therefore articles
            articles_categories_path:
                provider: [specified, { path: /cms/routes }]
                exists_action: use
                not_exists_action: throw_exception
        #category name
        content_name:
            provider: [content_method, { method: getTitle }]
            exists_action: use
            not_exists_action: create
文章文档的

getRouteNodeCategory() 只返回一个父级(即类别)名称。以下是本文档的部分内容:

public function getRouteNodeCategory()
{
    return $this->parent->getTitle();
}

【问题讨论】:

    标签: php symfony-cmf


    【解决方案1】:

    目前在 RoutingAutoBundle 中这是不可能的,但它可以很容易地实现。

    RoutingAutoBundle 创建扩展 CMF 的 Route 对象的 Route 文档。这些 Route 对象确实支持可变模式,它允许您指定动态参数:

    https://github.com/symfony-cmf/RoutingBundle/blob/1.2/Model/Route.php#L53

    因此,我们只需要扩展映射以在 URL 中包含有关动态(可变)参数的详细信息。

    另请注意,新版本的路由自动包即将发布,它具有更好的配置格式。

    如果您可以创建一个详细说明您的用例的问题,我们将尝试在 1.0 版本中实施它。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多