How does symfony read configuration information?

WBOY
Release: 2016-08-04 09:21:42
Original
1367 people have browsed it

For example, the default configuration of the paging class

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>
Copy after login
Copy after login

I customized a function in the controller to see what parameters are there

I found that some configuration information is in the parameterBag and some is not. For example:
$this->getParameter('knp_paginator.page_range') has a value of 5
$this->getParameter('knp_paginator.default_options') An error will be reported directly
I don’t know what’s going on
What should I write if I want to read knp_paginator.default_options.page_name

Reply content:

For example, the default configuration of the paging class

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>
Copy after login
Copy after login
I customized a function in the controller to see what parameters are there

I found that some configuration information is in the parameterBag and some is not. For example:

$this->getParameter('knp_paginator.page_range') has a value of 5
$this->getParameter('knp_paginator.default_options') An error will be reported directly
I don’t know what’s going on
What should I write if I want to read knp_paginator.default_options.page_name

After checking the KnpPaginatorExtension class corresponding to the Bundle, I found that the default_options corresponding to the Bundle did not use the setParameter method of the container to declare parameters, so it cannot be obtained through the getParameter method of the container (if any expert finds out how to obtain it, please tell me).

If you want to get relevant parameters, you can do this.

Declare the corresponding parameters in parameters.yml:

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

Then use it like this in 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>
Copy after login

Simply extract the parameters into the parameters.yml file, and then assign the entire parameter object to default_options. This is ok. Then if you want to get the corresponding parameter value, you can directly use $this->getParameter('knp_paginator_options ')['page_name'] obtained.

Hope it helps you~

First of all, your understanding of

Symfony

configuration is wrong! First, the

$this->getParameter()

method, as its name suggests, the content read must be the parameters under the parameters field. The parameters you want to read are not necessarily under the parameters field. Further exploration is needed (first of all, you should check the official documentation for instructions in this regard, if not, then read on). Second,

KnpPaginatorBundle

The configuration of this third-party Bundle is loaded through this class, which is the dependency injection extension class of Bundle (Dependency Injection (DI) Extension) KnpBundlePaginatorBundleDependencyInjectionKnpPaginatorExtension . The following code is excerpted from the load method of this class. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;code&gt;$container-&gt;setParameter('knp_paginator.template.pagination', $config['template']['pagination']); $container-&gt;setParameter('knp_paginator.template.filtration', $config['template']['filtration']); $container-&gt;setParameter('knp_paginator.template.sortable', $config['template']['sortable']); $container-&gt;setParameter('knp_paginator.page_range', $config['page_range']);&lt;/code&gt;</pre><div class="contentsignin">Copy after login</div></div> Through the above code, you can see that this

Bundle

defines a total of four parameters. You can obtain these four parameters respectively through the $this->getParameter() method, as follows: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;code&gt;$this-&gt;getParameter('knp_paginator.template.pagination'); $this-&gt;getParameter('knp_paginator.template.filtration'); $this-&gt;getParameter('knp_paginator.template.sortable'); $this-&gt;getParameter('knp_paginator.template.page_range');&lt;/code&gt;</pre><div class="contentsignin">Copy after login</div></div> Finally, as for other parameters, since the

Bundle

author has not exposed them to developers in the form of parameters in the dependency injection extension class, we cannot obtain them through the $this->getParameter() method. Relevant document links are as follows: How to Load Service Configuration inside a Bundle

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template