PHP code example of vsavritsky / settingsbundle

1. Go to this page and download the library: Download vsavritsky/settingsbundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vsavritsky / settingsbundle example snippets


// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new vsavritsky\SettingsBundle\vsavritskySettingsBundle(),
        // ...
    );
}

namespace App\YourBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class DefaultController extends Controller
{
    /**
     * @Route("/")
     * @Template()
     */
    public function indexAction()
    {
        $settings = $this->get('settings');

        $title = $settings->get('page_title');
        $meta = $settings->group('meta');

        return array(
            'title' => $title,
            'meta_description' => $meta['description'],
            'meta_keywords' => $meta['keywords'],
        );
    }
}

$param1 = $settings->get('category', 'param1');
$param2 = $settings->get('category', 'param2');
// or
$cat = $settings->group('category');
$param1 = $cat['param1'];
$param2 = $cat['param2'];

// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new AppBundle\AppBundle(),
        new vsavritsky\SettingsBundle\vsavritskySettingsBundle(),
    );
}

$form = $this->createForm('vsavritsky_settings');
// $form->setData($setting); // use for edit of existed setting
if ($request->isMethod('POST')) {
    $form->handleRequest($request);
    if ($form->isValid()) {
        $this->get('settings')->save($form->getData());
    }
}
return array( 'form' => $form->createView() );

$form = $this->createForm('vsavritsky_settings_category');
if ($request->isMethod('POST')) {
    $form->handleRequest($request);
    if ($form->isValid()) {
        $this->get('settings')->saveGroup($form->getData());
    }
}
return array( 'form' => $form->createView() );

use vsavritsky\SettingsBundle\DBAL\SettingsType;

// In controller:

// Get service from container
$settings = $this->get('settings');

// Update a existed setting
$settings->update('param', 'new value');
$settings->update('category', 'param_in_cat', 'new value');

// Create a new setting
$settings->create(null, 'new.1', SettingsType::Boolean, true, 'comment - setting w/o group');
$settings->create('test', 'new.2', SettingsType::Text, 'test text', 'comment - setting in group');

// Create a new empty group
$settings->createGroup('new-cat', 'comment of group');
bash
$ php bin/console doctrine:schema:update --dump-sql
bash
$ php app/console doctrine:schema:update --dump-sql
twig
{% set params = settings_group('category') %}

<ul>
<li>{{ params.param1 }}</li>
<li>{{ params['param-2'] }}</li>
</ul>