fa-z-b/configuration-silex-provider

为 Silex 提供的简单配置服务提供者

1.1.0 2017-03-03 16:01 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:09:48 UTC


README

快速路由、查看和全局参数定义通过可读的结构化配置文件。

一旦选择您首选的可解析数据格式,注册 ConfigurationServiceProvider

# src/app.php

# $configuration = json_decode(file_get_contents(__DIR__ . '/Site/Resource/config/config.json'), true);
$configuration = Yaml::parse(file_get_contents(__DIR__ . '/Site/Resources/config/config.yml'));
$app->register(new ConfigurationServiceProvider(), array('site.config' => $configuration));

“settings” 键

允许您定义路由和视图变量。在“settings”键下的第一个键是您的路由名称。这里定义的所有路由都使用“Silex\ControllerCollection”进行注册。

# src/Site/Resources/config/config.yml

settings:
    homepage:
        route:
            controller:     default.controller:indexAction
            pattern:        /
            # other Silex route parameters...
        view:
            title:          homepage.title
            metas:
                keywords:  "keyword, keyword, keyword"
    contact:
        route:
            pattern:        /contact
            method:         GET|POST
        view:
            template:       page/contact.html
            title:          contact.title

如果没有提供控制器(例如:联系路由),将生成一个默认的闭包,返回给定的模板。

您可以通过以下语法在您的 twig 模板中访问配置

<h1>{{ app.config.get('homepage.view.title')|trans }}</h1>

为了方便,您可以使用 current 快捷方式来访问当前匹配路由的设置

<h1>{{ app.config.current('view.title')|trans }}</h1>

{# equivalent #}

<h1>{{ app.config.settings('homepage.view.title')|trans }}</h1>

同样也适用于控制器内部

public function indexAction(Request $request)
{
    $title = $this->container['config']->current('view.title', 'Default title');

    return $this->container['twig']->render('@site/Default/index.html');
}

“global” 键

这只是设置其他应用程序参数的约定。

# src/Site/Resources/config/config.yml
global:
    email:                  contact@localhost
    domain:                 localhost
    brand:                  brandname
    analytics_id:           UA-xxxxx
    facebook_url:           https://#/xxx
    linkedin_url:           https://www.linkedin.com/pub/xxx
    view:
        title:              " - default title suffix"
        metas:
            description:    "Silex app"

在 twig 模板中

# index.html

<title>{% block title %}{{ app.config.current('view.title') }}{% endblock %}{{ app.config.global('view.title')}}</title>

...

<a href="mailto:{{ app.config.global('email') }}">email me</a>

“settings” 和 “global” 键是保留的

一个 navigation 部分示例

settings:
...

navigation:
    homepage:
        label:              homepage.label
        childs:
            contact:
                label:      contact.label

在 twig 模板中

{{ app.config.navigation('current.label') }}

使用 bootstrap 框架的典型用例

{% import "@site/macro/navigation.html" as macros %}

<div id="navigation" class="navbar navbar-default navbar-fixed-top navbar-inverse shadow-bottom" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a href="{{ path('homepage') }}" class="navbar-brand" title="{{ app.config.global('brand')|trans }}">
        <span class="icon-logo"></span>
        <span class="suffix">{{ app.config.global('brand')|trans }}</span>
      </a>
    </div>
    <div id="scrollspy" class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        {{ macros.menu(app.config.navigation('homepage.childs')) }}
      </ul>
      {% if app.locales %}
      <ul class="nav navbar-nav navbar-right">
        {% for locale in app.locales %}
        <li{% if app.request.get('_locale') == locale %} class="active"{% endif %}>
          <a hreflang="{{locale}}" href="{{path(app.request.get('_route'), {_locale: locale})}}">{{locale|upper}}</a>
        </li>
        {% endfor %}
      </ul>
      {% endif %}

    </div>
  </div>
</div>

完整的 config.yml 示例

global:
    email:                  contact@localhost
    domain:                 localhost
    brand:                  brand.name
    analytics_id:           UA-xxxxx
    facebook_url:           https://#/xxx
    linkedin_url:           https://www.linkedin.com/pub/xxx
    view:
        title:              view.title
        suffix:             view.title.suffix
        metas:
            description:    view.metas.description
settings:
    homepage:
        route:
            controller: default.controller:indexAction
            pattern:    /
        view:
            title:      home.title
            metas:
                keywords:    ~
                description: ~
    contact:
        route:
            controller: default.controller:contactAction
            pattern:    /contact
        view:
            title:      contact.title
    test:
        route:
            pattern:    /test
            i18n:       false
        view:
            template:   '@site/Default/test.html'

navigation:
    homepage:
        label:          home.label
        childs:
            contact:
                label:  contact.label
            test:
                label:  test.label

F/\Z-B 2014