PHP code example of mitopp / schema-org-bundle

1. Go to this page and download the library: Download mitopp/schema-org-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/ */

    

mitopp / schema-org-bundle example snippets


return [
    // ...
    Mitopp\SchemaOrgBundle\MitoppSchemaOrgBundle::class => ['all' => true],
];

namespace App\Controller;

use Mitopp\SchemaOrgBundle\Config\SchemaOrgConfigurationInterface;
use Mitopp\SchemaOrgBundle\Graph\SchemaOrgGraphCollectorInterface;
use Mitopp\SchemaOrgBundle\Type\Thing\Organization;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class HomeController extends AbstractController
{
    #[Route('/', name: 'app_home')]
    public function index(SchemaOrgGraphCollectorInterface $collector, SchemaOrgConfigurationInterface $configuration): Response
    {
        $org = new Organization(
            identifier: $configuration->createIdentifier('#org'),
            name: 'My Company',
            url: $configuration->getBaseUrl(),
            logo: $configuration->createIdentifier('/logo.png'),
        );

        $collector->add($org);

        return $this->render('home/index.html.twig');
    }
}

namespace App\EventListener;

use Mitopp\SchemaOrgBundle\Config\SchemaOrgConfigurationInterface;
use Mitopp\SchemaOrgBundle\Graph\SchemaOrgGraphCollectorInterface;
use Mitopp\SchemaOrgBundle\Type\Thing\CreativeWork\WebSite;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

#[AsEventListener(event: KernelEvents::REQUEST, method: 'onKernelRequest')]
final class SchemaOrgListener
{
    public function __construct(
        private readonly SchemaOrgGraphCollectorInterface $collector,
        private readonly SchemaOrgConfigurationInterface $configuration,
    ) {
    }

    public function onKernelRequest(RequestEvent $event): void
    {
        if (!$event->isMainRequest()) {
            return;
        }

        $website = new WebSite(
            identifier: $this->configuration->createIdentifier('#website'),
            url: $this->configuration->getBaseUrl(),
            name: 'Example Website',
        );
        
        $this->collector->add($website);
    }
}

namespace App\Schema\Type;

use Mitopp\SchemaOrgBundle\Type\AbstractType;

class MyCustomType extends AbstractType
{
    public function __construct(string $identifier)
    {
        parent::__construct('MyCustomType', $identifier);
    }
    
    public function setCustomProperty(string $value): self
    {
        $this->data['customProperty'] = $value;
        
        return $this;
    }
}