kunicmarko / configuration-panel
这是一个用于向您的 sonata admin 添加配置面板的 Symfony Bundle。
Requires
- php: ^5.6|^7.0
- symfony/framework-bundle: ~2.8|~3.0
This package is not auto-updated.
Last update: 2022-02-01 13:03:50 UTC
README
ABANDONED, Please use SimpleConfigurationBundle
此包向您的 sonata admin 添加配置面板,您还可以轻松扩展包并添加自己的类型。
此包依赖于 SonataAdminBundle
文档
安装
1. 将以下内容添加到 composer.json 文件中的 require
键
composer require kunicmarko/configuration-panel
2. 在 app/AppKernel.php
中注册该包
$bundles = array(
// ...
new KunicMarko\SonataConfigurationPanelBundle\ConfigurationPanelBundle(),
);
如果您未使用自动映射,请将其添加到您的 ORM 映射中
# app/config/config.yml
orm:
entity_managers:
default:
mappings:
AppBundle: ~
...
ConfigurationPanelBundle: ~
3. 更新数据库
app/console doctrine:schema:update --force
4. 清除缓存
app/console cache:clear
如何使用
在您的 twig 模板中,您可以调用它如下
{{ configuration.getAll() }}
{{ configuration.getValueFor(name) }}
如果您想在控制器中使用它,可以这样做
$this->get('configuration_panel.global.service')->getAll()
$this->get('configuration_panel.global.service')->getValueFor()
添加新类型
如果您想添加新类型,可以这样做
# app/config/config.yml
configuration_panel:
types:
newtype: YourBundle\Entity\NewType
创建新类型
您的新类型必须扩展 AbstractConfiguration,您还必须指定用于 sonata 列表(可以是 sonata 模板或您自己创建的)的模板,以及如何在表单中渲染字段。
1. 无新列的新类型
namespace YourBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataConfigurationPanelBundle\Entity\AbstractConfiguration;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\TextType;
/**
*
* @ORM\Entity(repositoryClass="KunicMarko\SonataConfigurationPanelBundle\Repository\ConfigurationRepository")
*
*/
class NewType extends AbstractConfiguration
{
/**
* {@inheritDoc}
*/
public function getTemplate()
{
return 'SonataAdminBundle:CRUD:list_string.html.twig';
}
/**
* {@inheritDoc}
*/
public function generateFormField(FormMapper $formMapper)
{
$formMapper->add('value', TextType::class, ['required' => false]);
}
}
2. 带新列的新类型
如以下示例代码所示,我们添加了新的 $date
字段,必要的事情是将 getValue()
方法覆盖,委托到新字段的下述 getter,如下所示。
namespace YourBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataConfigurationPanelBundle\Entity\AbstractConfiguration;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\DateType;
/**
*
* @ORM\Entity(repositoryClass="KunicMarko\SonataConfigurationPanelBundle\Repository\ConfigurationRepository")
*
*/
class NewType extends AbstractConfiguration
{
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="date", nullable=true)
*/
private $date;
/**
* Set date
*
* @param \DateTime $date
*
* @return DateType
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Get date
*
* @return \DateTime
*/
public function getValue()
{
return $this->getDate();
}
/**
* {@inheritDoc}
*/
public function getTemplate()
{
//return 'SonataAdminBundle:CRUD:list_string.html.twig'; can also be used
return 'ConfigurationPanelBundle:CRUD:list_field_date.html.twig';
}
/**
* {@inheritDoc}
*/
public function generateFormField(FormMapper $formMapper)
{
$formMapper->add('date', DateType::class, ['required' => false]);
}
}
添加新字段后,不要忘记更新数据库
app/console doctrine:schema:update --force
角色和类别
此包是为开发者制作的辅助工具,因此只有 ROLE_SUPER_ADMIN
可以创建和删除项目,普通管理员只能编辑。(您可以创建密钥并允许其他管理员仅编辑它们)。创建项目时有两个类别,Meta
和 General
,只有 ROLE_SUPER_ADMIN
可以查看和编辑 META
类别的项目,而普通管理员只能编辑和查看 General
项目。
其他内容
当包含此包时,您将能够访问我需要的某些 twig 过滤器。
已过时间
在twig中,您可以使用|elapsed
过滤器,这将返回易于阅读的时间,它适用于时间戳或DateTime对象。
{{ var|elapsed }}
#outputs "time" ago, 5 days ago, 5 minutes ago, just now, 1 month ago, etc.