定制 vctls/select2entity-extension-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vctls/select2entity-extension-bundle

最新稳定版本:1.0

Composer 安装命令:

composer require vctls/select2entity-extension-bundle

包简介

An extension of TetranzSelect2EntityBundle with a couple helper classes.

README 文档

README

This bundle provides helpers for working with Tetranz Select2EntityBundle.

Searcher classes define configuration for the entities used in Select2 fields.

Configuration

Add routing to the default controller:

# routes.yml
vctls_select2_entity_extension:
    resource: '@VctlsSelect2EntityExtensionBundle/Resources/config/routing.yaml'

Add the namespace of your Searcher classes in config.yml. Default is App\Util:

# config.yml
vctls_select2_entity_extension:
    searcher_namespace: 'App\Tools\Select2\'

Creating Select2 fields:

First, create a Searcher class for the entity you want to use in your field.

<?php

namespace App\Util;

use Vctls\Select2EntityExtensionBundle\Util\Searcher;

class ExampleSearcher extends Searcher
{
    public $alias = 'example';
    public $idMethod = 'getId';
    public $textMethod = '__toString';
    public $searchfileds = ['example.name'];
}

Then, add a Select2 field in your form:

<?php

namespace App\Form\Example;

use App\Entity\Example;
use Vctls\Select2EntityExtensionBundle\Util\S2;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class ExampleType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // with the helper class...
        $builder->add(...S2::build('example', Example::class));
        
        // or without it
        $builder->add('example2', Select2EntityType::class, [
            'remote_route' => 'generic_autocomplete',
            'remote_params' => [
                'classname' => Example::class
            ],
            'class' => Example::class,
            'primary_key' => 'id',
            'text_property' => 'name',
            'minimum_input_length' => 2,
            'delay' => 250,
            'cache' => true,
            'placeholder' => 'Select an example',
        ]);
    }
}

Secure access to the default controller

You can create a Voter that will controll access to different entities.

<?php
// App\Security\EntityVoter.php
namespace App\Security;

use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use App\Entity;

class EntityVoter extends Voter
{
    const ENTITIES_BY_ROLE = [
        'ROLE_USER' => [
            Entity\Example::class
        ],
        'ROLE_ADMIN' => [
            Entity\User::class
        ]
    ];
    
    protected function supports($access, $subject)
    {
        return $access === 'view' && gettype($subject) === 'string';
    }

    protected function voteOnAttribute($access, $subject, TokenInterface $token)
    {
        $roles = $token->getRoles();

        foreach ($roles as $role) {
            if (in_array($subject, self::ENTITIES_BY_ROLE[$role->getRole()])) {
                return true;
            }
        }   
        return false;
    }
}

统计信息

  • 总下载量: 1.56k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固