2017 © Pedro PelĂĄez
 

symfony-bundle cmf-routing-extra-bundle

Symfony Positibe CmfRoutingExtra Bundle

image

positibe/cmf-routing-extra-bundle

Symfony Positibe CmfRoutingExtra Bundle

  • Tuesday, December 5, 2017
  • by pcabreus
  • Repository
  • 1 Watchers
  • 1 Stars
  • 3 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 18 Versions
  • 0 % Grown

The README.md

PositibeCmfRoutingExtraBundle

The PositibeCmfRoutingExtraBundle add Doctrine ORM support for Symfony CmfRoutingBundle to store routing on orm databses, (*1)

Installation

To install the bundle just add the dependent bundles:, (*2)

php composer.phar require positibe/cmf-routing-extra-bundle

Next, be sure to enable the bundles in your application kernel:, (*3)

<?php
// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Symfony\Cmf\Bundle\RoutingBundle\CmfRoutingBundle(),
        new Symfony\Cmf\Bundle\RoutingAutoBundle\CmfRoutingAutoBundle(),
        new Positibe\Bundle\CmfRoutingExtraBundle\PositibeCmfRoutingExtraBundle(),

        // ...
    );
}

Configuration

Copy the configuration in your configurations packages:, (*4)

# config/packages/positibe_routing
parameters:
#    locales: [es, en, fr] # Maybe you already have it configured

cmf_routing:
    chain:
        routers_by_id:
            cmf_routing.dynamic_router: 200
            router.default: 100
    dynamic:
        enabled: true
        uri_filter_regexp: "#^(?!(/css/|/js/|/admin/|/security/|/frontend/|/backend/|/public/)).*$#"
        persistence:
            orm:
                route_class: 'Positibe\Bundle\CmfRoutingExtraBundle\Entity\AutoRoute'
#        route_provider_service_id: positibe_routing.route.provider
        generic_controller: PositibeCmfRoutingExtraBundle:GenericContent:index

cmf_routing_auto:
    adapter: positibe_doctrine_orm

Caution:: This bundle use the timestampable, sluggable, translatable and sortable extension of GedmoDoctrineExtension. Be sure that you have the listeners for this extensions enable. You can also to use StofDoctrineExtensionBundle., (*5)

Remember to update the schema:, (*6)

php app/console doctrine:schema:update --force

Using

An entity that has routes must implement Symfony\Cmf\Component\Routing\RouteReferrersInterface., (*7)

Add to any entity you want the relation with Positibe\Bundle\CmfRoutingExtraBundle\Entity\AutoRoute and the needed methods:, (*8)

<?php
// src/AppBundle/Entity/Post.php
namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Cmf\Component\Routing\RouteReferrersInterface;
use Doctrine\ORM\Mapping as ORM;

/**
 *
 * @ORM\Table(name="app_post")
 * @ORM\Entity(repositoryClass="AppBundle\Entity\PostRepository")
 */
class Post implements RouteReferrersInterface {

    /**
     * @var ArrayCollection|RouteObjectInterface[]
     *
     * @ORM\ManyToMany(targetEntity="Positibe\Bundle\CmfRoutingExtraBundle\Entity\AutoRoute", orphanRemoval=TRUE, cascade="all")
     * @ORM\JoinTable(name="app_post_routes")
     */
    protected $routes;

    public function __construct()
    {
        $this->routes = new ArrayCollection();
    }

    /**
     * @return ArrayCollection|\Symfony\Cmf\Component\Routing\RouteObjectInterface[]
     */
    public function getRoutes()
    {
        return $this->routes;
    }

    /**
     * @param ArrayCollection|\Symfony\Cmf\Component\Routing\RouteObjectInterface[] $routes
     */
    public function setRoutes($routes)
    {
        $this->routes = $routes;
    }

    /**
     * Add a route to the collection.
     *
     * @param \Symfony\Component\Routing\Route $route
     * @return $this
     */
    public function addRoute($route)
    {
        $this->routes[] = $route;

        return $this;
    }

    /**
     * Remove a route from the collection.
     *
     * @param \Symfony\Component\Routing\Route $route
     */
    public function removeRoute($route)
    {
        $this->routes->removeElement($route);
    }
}

Tip: You can use Positibe\Bundle\CmfRoutingExtraBundle\Entity\HasRoutesTrait to simplify the implementation of RouteReferrerInterface methods and mapping. This create a many to many relation without doing nothing more., (*9)

<?php
// src/AppBundle/Entity/Post.php
namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Positibe\Bundle\CmfRoutingExtraBundle\Entity\HasRoutesTrait;
use Doctrine\ORM\Mapping as ORM;

/**
 *
 * @ORM\Table(name="app_post")
 * @ORM\Entity(repositoryClass="AppBundle\Entity\PostRepository")
 */
class Post implements RouteReferrersInterface {

    use HasRoutesTrait;

    public function __construct()
    {
        $this->routes = new ArrayCollection();
    }
}

Creating routes

$post = new Post(); //Class that implement `Symfony\Cmf\Component\Routing\RouteReferrersInterface`
$post->setTitle('You're awesome'); //Fill datas
$manager->persist($post);
$manager->flush(); //Flush to be able to take the id of the `$post`

$contentRepository = $this->container->get('cmf_routing.content_repository');
$route = new AutoRoute(); //Class of `Positibe\Bundle\CmfRoutingExtraBundle\Entity\AutoRoute`
$route->setStaticPrefix('/you-are-awesome'); //Set the permalink of post instance
$route->setDefault(RouteObjectInterface::CONTENT_ID, $contentRepository->getContentId($post)); this set ``FQN:id`` into ``content_id``
$route->setContent($post);
$post->addRoute($route);

$em->persist($post);
$em->flush();

Content with Custom Routing

If your content implement Positibe\Bundle\CmfRoutingExtraBundle\Model\CustomRouteInterface, you can update all your routes with the selected controller, without the need of do it on each one., (*10)

[yaml]
# app/config/config.yml
positibe_cmf_routing_extra:
    controllers:
        homepage:
            _controller: [FrameworkBundle:Template:template, {template: "index.html.twig"}]
        default:
            _controller: [AppBundle:Default:index, {}]

You have the access to this config through positibe_cmf_routing_extra.route_factory., (*11)

Creating automatic routes

See on auto_routing.md., (*12)

For more information see the Symfony Cmf Routing Bundle Documentation, (*13)

The Versions