2017 © Pedro Peláez
 

library configuration-silex-provider

A Simple configuration service provider for Silex

image

faz-b/configuration-silex-provider

A Simple configuration service provider for Silex

  • Friday, March 3, 2017
  • by r1K0
  • Repository
  • 1 Watchers
  • 0 Stars
  • 44 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Simple configuration for Silex App

Rapid routing, view and global parameters definition through a human readable structured config file., (*1)

Once you choose your preferred parsable data format, register the ConfigurationServiceProvider., (*2)

# 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));

The settings key

Allows to define your routes and view variables. The first key, under the settings key is your route name. All routes defined here are registered with the Silex\ControllerCollection., (*3)

# 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

When no controller is given (eg: contact route), a default closure will be generated, returning the given template., (*4)

The configuration can be accessed in your twig template through the following syntax :, (*5)

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

As a convenience, you can use the current shortcut to access the settings for the current matched route, (*6)

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

{# equivalent #}

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

The same applies inside a controller, (*7)

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

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

The global key

It is just a convention for settings your others application parameters., (*8)

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

In a twig template:, (*9)

# 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>

the settings and global keys are reserved, (*10)

A navigation section example

settings:
...

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

In a twig template:, (*11)

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

Typical usecase using the bootstrap framework

{% 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>

Full config.yml example

global:
    email:                  contact@localhost
    domain:                 localhost
    brand:                  brand.name
    analytics_id:           UA-xxxxx
    facebook_url:           https://www.facebook.com/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, (*12)

The Versions

03/03 2017

dev-master

9999999-dev

A Simple configuration service provider for Silex

  Sources   Download

MIT

The Requires

 

php configuration silex

03/03 2017

1.1.0

1.1.0.0

A Simple configuration service provider for Silex

  Sources   Download

MIT

The Requires

 

php configuration silex

21/04 2016

1.0.1

1.0.1.0

A Simple configuration service provider for Silex

  Sources   Download

MIT

The Requires

 

php configuration silex

20/04 2016

1.0.0

1.0.0.0

A Simple configuration service provider for Silex

  Sources   Download

MIT

The Requires

 

php configuration silex