承接 app-verk/block-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

app-verk/block-bundle

最新稳定版本:v1.0

Composer 安装命令:

composer require app-verk/block-bundle

包简介

AppVerk block bundle

README 文档

README

Symfony Block Bundle

Configure

Require the bundle with composer:

$ composer require app-verk/block-bundle

Enable the bundle in the kernel:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new AppVerk\BlockBundle\BlockBundle(),
        // ...
    );
}

Twig helper

Render a block:

{{ render_block(block_id) }}

Render a block with options:

{{ render_block(block_id, {
    'template': 'BlockBundle:Block:sample.html.twig'
}) }}

Create Block:

Remember to extends AppVerk\BlockBundle\Block\AbstractBlock.

<?php

namespace AppBundle\Block;

use AppVerk\BlockBundle\Block\AbstractBlock;
use Symfony\Component\OptionsResolver\OptionsResolver;

class HelloBlock extends AbstractBlock
{
    protected function configureOptions(OptionsResolver $resolver)
    {
        parent::configureOptions($resolver);

        $resolver->setDefaults([
            'template' => 'AppBundle:Block:hello.html.twig',
            'message'  => null
        ]);
    }
}

Block should be set as public service or have alias.

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

    AppBundle\:
        resource: '../../*'
        exclude: '../../{Entity,Repository,Tests,Doctrine,Twig}'

    AppBundle\Block\HelloBlock:
        public: true

Execute in Twig:

{{ render_block('AppBundle\\Block\\HelloBlock', {
    'template': 'AppBundle:Block:hello.html.twig',
    'message': 'Hello there!'
}) }}

More complicated Blocks can override execute method and used for example EntityManager:

<?php

namespace AppBundle\Block;

use AppBundle\Entity\Product;
use AppVerk\BlockBundle\Block\AbstractBlock;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityNotFoundException;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ProductBlock extends AbstractBlock
{
    /**
     * @var EntityManagerInterface
     */
    private $entityManager;


    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    protected function configureOptions(OptionsResolver $resolver)
    {
        parent::configureOptions($resolver);

        $resolver->setDefaults([
            'template' => 'AppBundle:Block:product.html.twig',
            'slug'     => ''
        ]);
    }

    public function execute(array $options = [])
    {
        $settings = $this->getSettings($options);

        $entity = $this->entityManager->getRepository(Product::class)->findOneBy([
            'slug' => $settings['slug']
        ]);
        if (!$entity) {
            throw new EntityNotFoundException();
        }

        return $this->renderResponse($settings['template'], [
            'settings' => $settings,
            'entity'   => $entity
        ]);
    }
}

License

The bundle is released under the MIT License.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-06-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固