How does symfony read configuration information?

Source: Internet
Author: User
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, youSymfonyIncorrect configuration understanding!

First,$this->getParameter()As the name suggests, the read content must beparametersParameters under the field. The parameter you want to read is not necessarily inparametersField. Further exploration is needed (first, you should check the description in the official documentation. if not, read it ).

Second,KnpPaginatorBundleThis third partyBundleIs configured through this class, that isBundleDependency Injection extension class (Dependency Injection (DI) Extension)Knp\Bundle\PaginatorBundle\DependencyInjection\KnpPaginatorExtension. The following code is taken fromloadMethod.

$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 thatBundleA 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 parametersBundleThe 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

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.