Heim  >  Artikel  >  Backend-Entwicklung  >  symfony如何读取配置信息?

symfony如何读取配置信息?

WBOY
WBOYOriginal
2016-08-04 09:21:421329Durchsuche

比如分页类默认的配置

knp_paginator:

<code>page_range: 5
default_options:
    page_name: page111                # page query parameter name
    sort_field_name: sort          # sort field query parameter name
    sort_direction_name: direction # sort direction query parameter name
    distinct: true                 # ensure distinct results, useful when ORM queries are using GROUP BY statements
template:
    pagination: KnpPaginatorBundle:Pagination:sliding.html.twig     # sliding pagination controls template
    sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template
</code>

我在控制器中自定义了一个函数查看都有哪些parameters

发现有的配置信息在parameterBag里面而有的没有
例如:
$this->getParameter('knp_paginator.page_range') 就有值 是 5
$this->getParameter('knp_paginator.default_options') 就会直接报错
不知道是咋回事
如果我想读取 knp_paginator.default_options.page_name 应该怎么写

回复内容:

比如分页类默认的配置

knp_paginator:

<code>page_range: 5
default_options:
    page_name: page111                # page query parameter name
    sort_field_name: sort          # sort field query parameter name
    sort_direction_name: direction # sort direction query parameter name
    distinct: true                 # ensure distinct results, useful when ORM queries are using GROUP BY statements
template:
    pagination: KnpPaginatorBundle:Pagination:sliding.html.twig     # sliding pagination controls template
    sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template
</code>

我在控制器中自定义了一个函数查看都有哪些parameters

发现有的配置信息在parameterBag里面而有的没有
例如:
$this->getParameter('knp_paginator.page_range') 就有值 是 5
$this->getParameter('knp_paginator.default_options') 就会直接报错
不知道是咋回事
如果我想读取 knp_paginator.default_options.page_name 应该怎么写

查看了该Bundle对应的KnpPaginatorExtension类之后,发现这个Bundle对应的default_options并没有使用容器的setParameter方法进行参数声明,所以无法通过容器的getParameter方法获取(如果哪位大神发现获取方法也可以告诉我)。

如果你想要获取相关参数,可以这样做。

在parameters.yml中声明对应的参数:

<code>knp_paginator_options:
        page_name: custom_page_name
        sort_field_name: sort
        sort_direction_name: direction
        distinct: true</code>

然后在config.yml中这样使用

<code>knp_paginator:
    page_range: 5
    default_options: "%knp_paginator_options%"
    template:
        pagination: KnpPaginatorBundle:Pagination:sliding.html.twig     # sliding pagination controls template
        sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template</code>

简单把参数抽离到parameters.yml文件中,然后把整个参数对象赋值给default_options,这样就ok了,然后如果你想获取对应的参数值,就可以直接使用$this->getParameter('knp_paginator_options')['page_name']获取了。

希望能帮到你~

首先,你对Symfony配置的理解有误!

第一,$this->getParameter()方法顾名思义,读取的内容必须是parameters字段下的参数。你想读取的参数不一定就在parameters字段下。还需要进一步探究(首先应该查看官方文档这方面的说明,如果没有,则往下看)。

第二,KnpPaginatorBundle这个第三方Bundle的配置是通过这个类,也就是Bundle的依赖注入扩展类(Dependency Injection (DI) ExtensionKnp\Bundle\PaginatorBundle\DependencyInjection\KnpPaginatorExtension来载入的。以下代码摘自该类的load方法。

<code>$container->setParameter('knp_paginator.template.pagination', $config['template']['pagination']);
$container->setParameter('knp_paginator.template.filtration', $config['template']['filtration']);
$container->setParameter('knp_paginator.template.sortable', $config['template']['sortable']);
$container->setParameter('knp_paginator.page_range', $config['page_range']);</code>

通过以上代码,可以看出,这个Bundle一共定义了四个参数,你可以通过$this->getParameter()方法分别获取这四个参数,如下:

<code>$this->getParameter('knp_paginator.template.pagination');
$this->getParameter('knp_paginator.template.filtration');
$this->getParameter('knp_paginator.template.sortable');
$this->getParameter('knp_paginator.template.page_range');</code>

最后,至于其他参数,由于Bundle作者没有在依赖注入扩展类中以parameter的形式暴露给开发者,我们是无法通过$this->getParameter()方法获取的。

相关文档链接如下:How to Load Service Configuration inside a Bundle

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn