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>
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
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>
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
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>
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>
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
First, the 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,
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"><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></pre><div class="contentsignin">Copy after login</div></div>
Through the above code, you can see that this
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"><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></pre><div class="contentsignin">Copy after login</div></div>
Finally, as for other parameters, since the
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