The ModularRouting component makes it easy to configure dynamic routes by defining reusable modules. ModularBundle integrates this functionality directly into the Symfony framework. This page shows how to configure the bundle by using Symfony CMF's RoutingBundle to enable the use of multiple routers.

Installation

Download the bundle

Start by requiring the bundles in Composer:

$ composer require symfony-cmf/routing-bundle "~2.0"
$ composer require harmony-project/modular-bundle "dev-master"

Composer will install the required libraries into your vendor directory and add the dependencies to your composer.json file.

Enable the bundles

Enable the required bundles by registering them in your AppKernel class:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = [
        // ...
        new \Symfony\Cmf\Bundle\RoutingBundle\CmfRoutingBundle(),
        new \Harmony\Bundle\ModularBundle\HarmonyModularBundle(),
        // ...
    ];
}

Create the module entity

Create a module entity by implementing ModuleInterface or by extending the base Module class provided by the ModularRouting component. The router uses the modularType field of the module to match the correct routing metadata to the request. This is an example of how the module entity should look if you're using the Doctrine ORM:

<?php
// src/AppBundle/Entity/Module.php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Harmony\Component\ModularRouting\Model\Module as BaseModule;

/**
 * @ORM\Entity
 * @ORM\Table(name="module")
 */
class Module extends BaseModule
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="modular_type", type="string", length=255)
     */
    protected $modularType;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Get modular identity
     *
     * @return mixed
     */
    public function getModularIdentity()
    {
        return $this->getId();
    }

    // custom methods...
}

Configure the bundle

Add the following configuration to your project's configuration file:

# app/config/config.yaml

# Harmony Modular Configuration
harmony_modular:
  module_class: AppBundle\Entity\Module
  module_identifier: 'id'
  route_prefix: '/module'
  router:
    resource: '%kernel.root_dir%/config/modular_routing.yml'

Only the module_class option is required to activate the bundle. For a complete overview of the available configuration options, see the Configuration reference.

The CMF RoutingBundle does not need any configuration to function, since ModularRouter is enabled by default and will only be triggered after the default Symfony router. It's possible to change the priority of ModularRouter with it's service handle harmony_modular.router. See the CMF RoutingBundle documentation for an overview of the available configuration options.

Create the module metadata

Before being able to use a module, you need to tell the project of it's existence by creating metadata definitions. This is an example of what basic metadata definitions look like:

# app/config/modular_routing.yml

foo_module:
  name: "Foo Module"
  type: "foo"
  routing:
    - { resource: "@AppBundle/Controller/FooController.php", type: "annotation" }

bar_module:
  name: "Bar Module"
  type: "bar"
  routing:
    - { resource: "app/config/routing/bar_module.yml", type: "yml" }

# import module definitions
acme:
  resource: "@AcmeBundle/Resources/config/modular_routing.yml"
  type:     yaml

This page was last updated on 16 Jul 2017, at 15:53.