For example, the default configuration knp_paginator: {code...} of the paging class ...} I customized a function in the controller to check which parameters found some configuration information in parameterBag, and some did not, for example, $ this-& amp; gt; getParameter (& #039; knp_paginator.page_range & #039 ;)... for example, the default page type configuration
Knp_paginator:
page_range: 5default_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 statementstemplate: pagination: KnpPaginatorBundle:Pagination:sliding.html.twig # sliding pagination controls template sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template
I have customized a function in the controller to check which parameters are available.
Some configuration information is found in parameterBag, while others are not
For example:
$ This-> getParameter ('knp _ paginator. page_range ') has a value of 5
$ This-> getParameter ('knp _ paginator. default_options ') directly reports an error
I don't know what's going on.
What should I do if I want to read knp_paginator.default_options.page_name?
Reply content:
For example, the default page type configuration
Knp_paginator:
page_range: 5default_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 statementstemplate: pagination: KnpPaginatorBundle:Pagination:sliding.html.twig # sliding pagination controls template sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template
I have customized a function in the controller to check which parameters are available.
Some configuration information is found in parameterBag, while others are not
For example:
$ This-> getParameter ('knp _ paginator. page_range ') has a value of 5
$ This-> getParameter ('knp _ paginator. default_options ') directly reports an error
I don't know what's going on.
What should I do if I want to read knp_paginator.default_options.page_name?
After checking the KnpPaginatorExtension class corresponding to the Bundle, it is found that the default_options class corresponding to the Bundle does not use the setParameter method of the container for parameter declaration, therefore, it cannot be obtained through the getParameter method of the container (if any great God finds the retrieval method, he can also tell me ).
If you want to obtain the relevant parameters, you can do so.
Declare the corresponding parameters in parameters. yml:
knp_paginator_options: page_name: custom_page_name sort_field_name: sort sort_direction_name: direction distinct: true
And then use it in config. yml.
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
Simply extract the parameter from parameters. in the yml file, assign the entire parameter object to default_options, and then OK. if you want to obtain the corresponding parameter value, you can use $ this-> getParameter ('knp _ paginator_options ') ['page _ name'] to obtain the parameter.
Hope to help you ~
First, youSymfony
Incorrect configuration understanding!
First,$this->getParameter()
As the name suggests, the read content must beparameters
Parameters under the field. The parameter you want to read is not necessarily inparameters
Field. Further exploration is needed (first, you should check the description in the official documentation. if not, read it ).
Second,KnpPaginatorBundle
This third partyBundle
Is configured through this class, that isBundle
Dependency Injection extension class (Dependency Injection (DI) Extension
)Knp\Bundle\PaginatorBundle\DependencyInjection\KnpPaginatorExtension
. The following code is taken fromload
Method.
$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']);
The code above shows thatBundle
A total of four parameters are defined. you can use$this->getParameter()
Obtain the four parameters as follows:
$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');
Finally, as for other parametersBundle
The author has not been exposed to developers in the form of parameter in the dependency injection extension class. we cannot$this->getParameter()
Method.
Links to related documents are as follows: How to Load Service Configuration inside a Bundle