c975l / config-bundle
用于获取/设置 Symfony 应用的配置参数
Requires
- php: >=8.0
- c975l/includelibrary-bundle: *
- c975l/services-bundle: *
- c975l/toolbar-bundle: *
- symfony/form: *
- symfony/yaml: *
- dev-main
- v4.3
- v4.2.3
- v4.2.2
- v4.2.1
- v4.1
- v4.0.1
- v4.0
- 3.x-dev
- v3.0.2
- v3.0.1
- v3.0
- 2.x-dev
- v2.6
- v2.5.8.1
- v2.5.8
- v2.5.7
- v2.5.6
- v2.5.5
- v2.5.4
- v2.5.3
- v2.5.2
- v2.5.1
- v2.5
- v2.4.3
- v2.4.2
- v2.4.1
- v2.4
- v2.3.6.1
- v2.3.6
- v2.3.5
- v2.3.4
- v2.3.3
- v2.3.2
- v2.3.1
- v2.3
- v2.2.5
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1
- v2.0.1
- v2.0
- 1.x-dev
- v1.2.2
- v1.2.1.1
- v1.2.1
- v1.2
- v1.1.1
- v1.1
- v1.0
- dev-dev
This package is auto-updated.
Last update: 2024-09-12 10:20:42 UTC
README
请注意,此 Bundle >= v2.0 不使用 Configuration
类来构建参数的修改表单,而是使用自己定义的键值系统。有关使用 Configuration
类的用例,请参阅 1.x 分支。
ConfigBundle 执行以下操作
- 从 YAML 文件获取 Symfony 应用的配置参数定义,
- 构建一个表单,允许最终用户修改这些参数(权限检查由您自己进行),
- 提供 Twig 扩展,以便在 Twig 模板中获取这些参数值,
安装包
步骤 1:下载包
使用 Composer 安装库
composer require c975l/config-bundle
步骤 2:启用包
然后,通过将其添加到项目中 app/AppKernel.php
文件中注册的包列表中,启用该包
<?php class AppKernel extends Kernel { public function registerBundles() { $bundles = [ // ... new c975L\ConfigBundle\c975LConfigBundle(), ]; } }
步骤 3:覆盖模板
强烈建议使用 从第三方包覆盖模板功能 来完全集成到您的网站上。
为此,只需在您的应用程序中创建以下结构 templates/bundles/c975LConfigBundle/views/
,然后在该结构中复制 layout.html.twig
文件,以覆盖现有的包文件。
在 layout.html.twig
中,它将主要扩展您的布局并定义特定的变量,例如
{% extends 'layout.html.twig' %} {# Defines specific variables #} {% set title = 'Configuration' %} {% block content %} {% block config_content %} {% endblock %} {% endblock %}
使用方法
在您的包中,您需要创建一个文件 /config/bundle.yaml
(所需字段的描述)+ 控制器(访问配置表单的路由)+ 投票者(检查访问权限)即可!下面给出了代码示例。
在第一次使用之前,如果需要请求参数,您必须使用控制台命令 php bin/console config:create
从包的默认数据创建配置文件。
在更新配置时,将创建两个文件
config/config_bundles.yaml
包含定义字段的值,您必须将此文件添加到.gitignore
中,以确保不将数据(如 API 密钥)存储到公共/私有存储库中cache/dev|prod|test/configBundles.php
包含字段关联数组的关联数组'yourRoot.yourParameter' => 'value'
。
#Your config/bundle.yaml #Example of definition for parameter c975LEmail.roleNeeded yourRoot: #Name of your bundle without its 'Bundle' part, but including its vendor one, to keep its uniqueness, i.e. c975LEmail yourParameter: #The name or your parameter i.e. roleNeeded type: string #|bool|int|float|array|date required: true #|false default: "Your default value" #|null info: "Your description to help filling this parameter" #|null #The following options are specific for date type to define its range of years #startYear: 2010|current|null(or not set) #endYear: 2010|current|null(or not set) #In case you need to have common data shared, you can also add other roots with the scheme #yourCommonRoot #yourCommonParameter #...
然后是您的控制器文件
<?php //Your Controller file namespace App\Bundle\Controller; use Symfony\Component\Routing\Annotation\Route; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Exception\AccessDeniedException; use c975L\ConfigBundle\Service\ConfigServiceInterface; class YourController extends AbstractController { /** * @Route("/your_name/config", * name="your_name_config", * methods={"HEAD", "GET", "POST"}) */ public function config(Request $request, ConfigServiceInterface $configService) { //Add the case to your Voter $this->denyAccessUnlessGranted('config', 'yourDataIfNeeded'); $form = $configService->createForm('vendor/bundle-name');//As defined in your composer.json $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { //Validates config $configService->setConfig($form); //Redirects return $this->redirectToRoute('the_route_you_want_to_redirect_to'); } //Renders the config form return $this->render('@c975LConfig/forms/config.html.twig', array( 'form' => $form->createView(), 'toolbar' => '@c975LEmail', //set false (or remove) if you don't use c975L/ToolbarBundle )); }
然后在浏览器中调用定义的路由,设置(或您的用户)配置参数。
在类内部获取参数
要在类内部获取参数,请使用以下代码
<?php namespace Your\NameSpace; use c975L\ConfigBundle\Service\ConfigServiceInterface; class YourClass { protected function yourMethod(ConfigServiceInterface $configService) { $parameter = $configService->getParameter('yourRoot.yourParameter'); /** * You can also get parameter using the bundle name as defined in your composer.json. * This case is used when the files "config_bundles.yaml" and "configBundles.php" are not yet created. * For example, the first time you use the config Route and your Voter needs to check with a parameter defined using ConfigBundle. * Using this optional variable will make ConfigBundle creating the requested config files, based on default values in "bundle.yaml". */ $parameter = $configService->getParameter('yourRoot.yourParameter', 'vendor/bundle-name'); } }
检查类内部是否已定义参数
要检查是否已定义参数,请使用 $configService->hasParameter('yourRoot.yourParameter')
。
获取容器的参数
您可以使用 $configService->getContainerParameter('parameter')
来访问容器的参数,并在 ConfigService
已注入时避免注入 Container
。
Twig 扩展
如果您需要在 Twig 模板中访问参数,请简单地使用 {{ config('yourRoot.yourParameter') }}
。
如果您需要在 Twig 模板中访问容器的参数,请简单地使用 {{ configParam('parameter') }}
。
如果此项目 帮助您减少开发时间,您可以通过顶部按钮“赞助”来赞助我 :)