Skip to content

Instantly share code, notes, and snippets.

@trq
Last active August 29, 2015 14:02
Show Gist options
  • Save trq/37af2910a917efea9ee7 to your computer and use it in GitHub Desktop.
Save trq/37af2910a917efea9ee7 to your computer and use it in GitHub Desktop.
<?php
namespace CellCare\SiteBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class CmsController extends Controller
{
public function dynamicAction()
{
return new Response("Hello CMS!");
}
}
cmf_routing:
chain:
routers_by_id:
cmf_routing.dynamic_router: 200
router.default: 100
dynamic:
enabled: true
route_provider_service_id: cell_care_site.routeProvider
default_controller: cell_care_site.controller.cms:dynamicAction
<?php
namespace CellCare\SiteBundle\Routing;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Util\ClassUtils;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Cmf\Component\Routing\RouteProviderInterface;
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\DoctrineProvider;
class RouteProvider extends DoctrineProvider implements RouteProviderInterface
{
/**
* {@inheritDoc}
*/
public function getRouteByName($name, $parameters = array())
{
if (is_object($name)) {
$className = ClassUtils::getClass($name);
if (isset($this->routeConfigs[$className])) {
return $this->createRouteFromEntity($name);
}
}
$repo = $this->getObjectManager()->getRepository('CellCareSiteBundle:Page');
$entity = $repo->findOneBy(['slug' => $name]);
if ($entity) {
return $this->createRouteFromEntity($entity);
}
throw new RouteNotFoundException("No route found for name '$name'");
}
/**
* {@inheritDoc}
*/
public function getRoutesByNames($names = null)
{
if (null === $names) {
if (0 === $this->routeCollectionLimit) {
return array();
}
$collection = new RouteCollection();
$repo = $this->getObjectManager()->getRepository('CellCareSiteBundle:Page');
$entities = $repo->findBy(array(), null, $this->routeCollectionLimit ?: null);
foreach ($entities as $entity) {
$name = $this->getFieldValue($entity, 'slug');
$collection->add($name, $this->createRouteFromEntity($entity));
}
return $collection;
}
$routes = array();
foreach ($names as $name) {
try {
$routes[] = $this->getRouteByName($name);
} catch (RouteNotFoundException $e) {
// not found
}
}
return $routes;
}
/**
* {@inheritDoc}
*/
public function getRouteCollectionForRequest(Request $request)
{
$path = $request->getPathInfo();
$collection = new RouteCollection();
if (empty($path)) {
return $collection;
}
$prefix = '';
$repo = $this->getObjectManager()->getRepository('CellCareSiteBundle:Page');
if ('' === $prefix || 0 === strpos($path, $prefix)) {
$name = substr($path, strlen($prefix));
$name = trim($name, '/');
$entity = $repo->findOneBy(['slug' => $name]);
if ($entity) {
$route = $this->createRouteFromEntity($entity);
if (preg_match('/.+\.([a-z]+)$/i', $name, $matches)) {
$route->setDefault('_format', $matches[1]);
}
$collection->add($name, $route);
}
}
return $collection;
}
/**
* @param $entity
* @param $fieldName
*
* @return string
*/
private function getFieldValue($entity, $fieldName)
{
return $entity->{'get'.ucfirst($fieldName)}();
}
/**
* @param $entity
*
* @return Route
*/
private function createRouteFromEntity($entity)
{
$prefix = '';
$className = ClassUtils::getClass($entity);
$fieldName = 'slug';
$value = $this->getFieldValue($entity, $fieldName);
$defaults = array('_entity' => $entity, $fieldName => $value);
return new Route($prefix . '/' . $value, $defaults);
}
}
parameters:
cell_care_site.controller.cms.class: CellCare\SiteBundle\Controller\CmsController
cell_care_site.routeProvider.class: CellCare\SiteBundle\Routing\RouteProvider
services:
cell_care_site.routeProvider:
class: %cell_care_site.routeProvider.class%
arguments: [@doctrine]
cell_care_site.controller.cms:
class: "%cell_care_site.controller.cms.class%"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment