首頁  >  文章  >  後端開發  >  symfony如何讀取設定資訊?

symfony如何讀取設定資訊?

WBOY
WBOY原創
2016-08-04 09:21:421329瀏覽

例如分頁類別預設的設定

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>

我在控制器中自訂了一個函數檢視有哪些parameters

發現有的設定資訊在parameterBag裡面而有的沒有
例如:
$this->getParameter('knp_paginator.page_range') 就有值是5
$this->getParameter('knp_paginator.default_options') 就會直接報錯
不知道是咋回事
如果我想讀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>

我在控制器中自訂了一個函數檢視有哪些parameters

發現有的設定資訊在parameterBag裡面而有的沒有
例如:
$this->getParameter('knp_paginator.page_range') 就有值是5
$this->getParameter('knp_paginator.default_options') 就會直接報錯
不知道是咋回事
如果我想讀knp_paginator.default_options.page_name 該怎麼寫

查看了該Bundle對應的KnpPaginatorExtension類別之後,發現這個Bundle對應的default_options並沒有使用容器的setParameter方法進行參數聲明,所以無法透過容器的getParameter方法取得(如果哪位大神發現取得方法也可以告訴我)。

如果你想要取得相關參數,可以這樣做。

在parameters.yml中聲明對應的參數:

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

然後在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>

簡單把參數抽離到parameters.yml檔案中,然後把整個參數物件賦值給default_options,這樣就ok了,然後如果你想取得對應的參數值,就可以直接使用$this->getParameter('knp_paginator_options' )['page_name']獲取了。

希望能幫到你~

首先,你對Symfony配置的理解有誤!

第一,$this->getParameter()方法顧名思義,讀取的內容必須是parameters字段下的參數。你想要讀取的參數不一定就在parameters欄位下。還需要進一步探究(首先應該查看官方文件這方面的說明,如果沒有,則往下看)。

第二,KnpPaginatorBundle這個第三方Bundle的配置是透過這個類,也就是Bundle的依賴注入擴充類別(Dependency Injection (DI) ExtenjoralatorKinecta)以下程式碼摘自該類別的load方法。

<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>
透過以上程式碼,可以看出,這個Bundle一共定義了四個參數,你可以透過

$this->getParameter()方法分別取得這四個參數,如下:

<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>
最後,至於其他參數,由於Bundle作者沒有在依賴注入擴展類中以parameter的形式暴露給開發者,我們是無法透過

$this->getParameter()方法獲取的。 相關文件連結如下:How to Load Service Configuration inside a Bundle

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn