vsavritsky / settingsbundle
Symfony4 Settings bundle 提供灵活的设置(布尔值、整数、浮点数、字符串、文本、HTML),可以轻松地通过 Sonata Admin 进行配置
Requires
- php: >=7.1
- fresh/doctrine-enum-bundle: >=4.8
- symfony/framework-bundle: >=4
Suggests
- egeloen/ckeditor-bundle: Allow use ckeditor for Html value
- sonata-project/admin-bundle: Allow configure settings with administration panel
This package is not auto-updated.
Last update: 2024-09-24 05:56:13 UTC
README
此包帮助您在 Symfony4 项目中管理设置。
设置有几种类型:布尔值、整数、浮点数、字符串、文本、HTML。您可以获取一个具体的设置或获取一组设置。设置的检索可能由项目中使用的缓存提供者进行缓存。
设置管理由 SonataAdminBundle 提供。在其他情况下,您可以通过使用特殊函数或预定义表单通过代码来管理设置。
安装
Composer
将 VsavritskySettingsBundle 及其依赖项下载到 vendor 目录。
您可以使用 Composer 进行自动化过程
$ composer require vsavritsky/settingsbundle
或将包链接手动添加到您的 composer.json
并运行 $ composer update
{ "require" : { "vsavritsky/settingsbundle": "~1.0" } }
Composer 将将包安装到 vendor/vsavritsky/settingsbundle
目录。
将包添加到您的应用程序内核
// app/AppKernel.php public function registerBundles() { $bundles = array( // ... new vsavritsky\SettingsBundle\vsavritskySettingsBundle(), // ... ); }
配置
该包不需要任何必需参数,并且无需在 config.yml
中进行更改即可运行。但您可以配置一些参数,下面将详细介绍。
现在您需要在您的数据库中创建表
$ php bin/console doctrine:schema:update --dump-sql
或 Symphony2
$ php app/console doctrine:schema:update --dump-sql
这将显示创建数据库中表的 SQL 查询。您可以手动运行这些查询。
注意。 您也可以执行
php bin/console doctrine:schema:update --force
命令,然后 Doctrine 将为您创建所需的表。但我强烈建议您首先执行--dump-sql
并检查 Doctrine 将执行的 SQL。
注意。 如果您使用的是 1.1.* 版本的包,您需要更新数据库。
用法
使用 SonataAdminBundle 来管理您的设置。否则使用预定义的表单。如果您使用数据库工具(phpMyAdmin 或其他)或通过在代码中调用特殊函数来配置设置,则可以自由使用此包。
您可以将设置放入组中,也可以不放入组中。组可用于在一次查询中检索多个设置。
设置的检索支持在 twig 模板或控制器(或在任何注入了设置服务的脚本)中。
例如,您创建了 3 个设置
page_title
没有组description
和keywords
在meta
组中。
在控制器中使用
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'], ); } }
在模板中使用
{% extends '::base.html.twig'%} {% block meta %} <title>{{ settings('page_title') }}</title> <meta name="description" content="{{ settings('meta', 'description') }}"> <meta name="keywords" content="{{ settings('meta', 'keywords') }}"> {% endblock %}
高级用法
完整配置
以下是该包的默认配置(所有参数都是可选的)
vsavritsky_settings: enable_short_service: true # default true, use false for disable registering 'settings' service html_widget: ckeditor # default null, valid values are 'null', 'ckeditor' cache_provider: cache.app # default null, for enable database caching set up name of caching service use_category_comment: false # default false, use category comment as its title in settings list (in SettingsAdmin) ckeditor: # set parameters of ckeditor. Not need if IvoryCKEditorBundle is installed base_path: /ckeditor/ js_path: /ckeditor/ckeditor.js
ckeditor
表单类型可以通过 IvoryCKEditorBundle 添加。如果您在没有 IvoryCKEditorBundle
的情况下使用 CKEditor,则必须指定 base_path
和 js_path
参数。
设置组
要从单个组中检索多个设置,您可以使用以下两种情况之一
$param1 = $settings->get('category', 'param1'); $param2 = $settings->get('category', 'param2'); // or $cat = $settings->group('category'); $param1 = $cat['param1']; $param2 = $cat['param2'];
两种情况都具有相同的性能 - 在第一次访问时,将检索整个组,数据检索只进行一次。
在 twig 中使用组
{% set params = settings_group('category') %} <ul> <li>{{ params.param1 }}</li> <li>{{ params['param-2'] }}</li> </ul>
使用设置组的示例
下面是使用组的示例 - 适用于后端和前端设置的设置。将多个设置放在 client
组中,并将其包含到模板中
{# app/Resources/views/base.html.twig #} {# ... #} <script> {% block javascript_inline %} window.settings = {{ settings_group('client')|json_encode(constant('JSON_NUMERIC_CHECK'))|raw }}; {% endblock %} </script> </body></html>
性能和缓存
使用缓存来增加获取设置。如果你还没有使用缓存 - 现在是完美的时机!这非常简单!
# app/config/services.yml services: cache.app: class: Symfony\Component\Cache\Adapter\FilesystemAdapter # app/config/config.yml vsavritsky_settings: cache_provider: cache.app
该套餐将使用已注册的服务 cache.app
来缓存数据。缓存提供者可以是以下之一 Doctrine 缓存 或 Symfony 缓存 - PSR-6 缓存(Symfony 3.1)或 PSR-16 简单缓存(Symfony 3.3)。
在 SonataAdminBundle 中排列设置管理员组
SonataAdminBundle
在 AppKernel::registerBundles()
中根据其包排列管理员组。如果 vsavritskySettingsBundle
被添加到你的应用包之上,那么设置组将是菜单中的第一个组(在你创建的内容或服务组之前)。如果你希望设置组是最后一个组(在你的组下面),你需要在 AppKernel::registerBundles()
中将 vsavritskySettingsBundle
添加到你的包之后。
// app/AppKernel.php public function registerBundles() { $bundles = array( // ... new AppBundle\AppBundle(), new vsavritsky\SettingsBundle\vsavritskySettingsBundle(), ); }
无 Sonata Admin 管理设置
如果你在项目中没有使用 SonataAdminBundle,你可以使用预定义的表单或特殊函数。
使用预定义的表单(在控制器中)
保存设置
$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() );
为了使用预定义的表单,你需要添加表单主题
# app/config/config.yml twig: # ... form_themes: - 'vsavritskySettingsBundle:Form:setting_value_edit.html.twig'
手动创建和更新设置
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');