Symfony How to read configuration information?

Source: Internet
Author: User
such as the default configuration of the paging class

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've customized a function in the controller to see what parameters

Found some configuration information in the Parameterbag and some not
For example:
$this->getparameter (' Knp_paginator.page_range ') has a value of 5.
$this->getparameter (' knp_paginator.default_options ') will directly error
I don't know what it is.
What should I write if I want to read knp_paginator.default_options.page_name?

Reply content:

such as the default configuration of the paging class

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've customized a function in the controller to see what parameters

Found some configuration information in the Parameterbag and some not
For example:
$this->getparameter (' Knp_paginator.page_range ') has a value of 5.
$this->getparameter (' knp_paginator.default_options ') will directly error
I don't know what it is.
What should I write if I want to read knp_paginator.default_options.page_name?

After viewing the corresponding Knppaginatorextension class of the bundle, it is found that the default_options of the bundle does not use the Setparameter method of the container for parameter declaration. So it is not possible to get through the GetParameter method of the container (if any of the great Gods find the method can also tell me).

If you want to get the relevant parameters, you can do so.

Declare the corresponding parameter 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 pull the parameters out of the Parameters.yml file, then assign the entire parameter object to default_options, so it's OK, and if you want to get the corresponding parameter value, you can use the $this->getparameter directly (' KNP _paginator_options ') [' page_name '] acquired.

Hope to help you ~

First of all, your Symfony understanding of the configuration is wrong!

First, the method as the $this->getParameter() name implies, the content must be read parameters under the parameters of the field. The parameters you want to read are not necessarily parameters under the field. Further research is needed (the official documentation should be reviewed first, if not, then look down).

Second, KnpPaginatorBundle this third Bundle -party configuration is loaded by this class, which is Bundle the dependency Injection extension Class ( Dependency Injection (DI) Extension ) Knp\Bundle\PaginatorBundle\DependencyInjection\KnpPaginatorExtension . The following code is excerpted from the methods of this class load .

$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']);

Through the above code, you can see that this Bundle altogether defines four parameters, you can $this->getParameter() get the four parameters by the method, 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 the other parameters, Bundle we cannot get through the method because the author is not exposed to the developer in Parameter form in the dependency injection extension class $this->getParameter() .

Links to related documents are as follows: How to Load Service Configuration inside a Bundle

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.