PHP code example of tourze / diy-page-bundle

1. Go to this page and download the library: Download tourze/diy-page-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / diy-page-bundle example snippets


// 4. Create your first block in controller or command
use DiyPageBundle\Entity\Block;
use DiyPageBundle\Entity\Element;

$block = new Block();
$block->setCode('welcome_banner')
      ->setTitle('Welcome Banner')
      ->setValid(true);

$element = new Element();
$element->setTitle('Welcome to Our Site!')
        ->setThumb1('/images/welcome.jpg')
        ->setPath('/welcome')
        ->setValid(true)
        ->setBlock($block);

$entityManager->persist($block);
$entityManager->persist($element);
$entityManager->flush();

// 5. Display in your template
$blocks = $rpcClient->call('GetDiyPageElementByCode', ['codes' => ['welcome_banner']]);

use DiyPageBundle\Entity\Block;
use DiyPageBundle\Entity\Element;

// Create a block
$block = new Block();
$block->setCode('homepage_banner')
      ->setTitle('Homepage Banner')
      ->setShowExpression('user.isVip or env.DEBUG')
      ->setStartTime(new \DateTime('2024-01-01'))
      ->setEndTime(new \DateTime('2024-12-31'));

// Add elements
$element = new Element();
$element->setTitle('New Year Sale')
        ->setImageUrl('/images/banner.jpg')
        ->setJumpPath('/promotion/newyear')
        ->setBlock($block);

$entityManager->persist($block);
$entityManager->persist($element);
$entityManager->flush();

// Using JSON-RPC interface
$response = $this->rpcClient->call('GetDiyPageElementByCode', [
    'codes' => ['homepage_banner', 'sidebar_ad']
]);

// Process returned data
foreach ($response as $code => $elements) {
    foreach ($elements as $element) {
        echo $element['title'];    // Element title
        echo $element['imageUrl']; // Image URL
        echo $element['jumpPath']; // Jump path
    }
}

// Based on user attributes
$block->setShowExpression('user.level >= 3');

// Based on environment variables
$block->setShowExpression('env.FEATURE_FLAG_ENABLED');

// Composite conditions
$block->setShowExpression('user.isVip and datetime.now >= "2024-01-01"');

use DiyPageBundle\Event\BlockDataFormatEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class BlockDataFormatSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            BlockDataFormatEvent::class => 'onBlockDataFormat',
        ];
    }

    public function onBlockDataFormat(BlockDataFormatEvent $event)
    {
        $data = $event->getData();
        $data['customField'] = 'customValue';
        $event->setData($data);
    }
}

$response = $rpcClient->call('GetDiyPageElementByCode', [
    'codes' => ['banner', 'sidebar'],
    'limit' => 10
]);

$response = $rpcClient->call('GetOneDiyPageElement', [
    'id' => 123
]);