Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update 'dynamic' routing based on names of content types and taxonomies #3095

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ frontend:
prefix: /
type: annotation

#
# Special loader for dynamic routes based on names of content types and taxonomies.
# Both for front-end and for the admin.
#
dynamic_routes:
resource: 'bolt.dynamic_route_loader::loadRoutes'
type: service

# ------------------------------------------------------------------------------
# Place your own routes here, that have a LOWER priority than the default routes.

Expand Down
3 changes: 3 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,6 @@ services:
decorates: 'api_platform.doctrine.orm.data_persister'

Symfony\Component\DependencyInjection\ContainerInterface: '@service_container'

bolt.dynamic_route_loader:
class: Bolt\Routing\DynamicRouteLoader
2 changes: 1 addition & 1 deletion src/Configuration/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private function getConfig(): Collection
$this->cache->delete(self::CACHE_KEY);
[$data] = $this->getCache();

// Clear the entire cache in order to re-generate %bolt.requirement.contenttypes%
// Clear the entire cache in order to re-generate routing table
$this->clearCacheController->clearcache($this->kernel);
}
}
Expand Down
15 changes: 3 additions & 12 deletions src/Controller/Backend/ContentEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Bolt\Repository\MediaRepository;
use Bolt\Repository\RelationRepository;
use Bolt\Repository\TaxonomyRepository;
use Bolt\Routing\DynamicRouteLoader;
use Bolt\Security\ContentVoter;
use Bolt\Utils\TranslationsManager;
use Bolt\Validator\ContentValidatorInterface;
Expand Down Expand Up @@ -133,25 +134,15 @@ public function edit(Content $content): Response
}

/**
* @Route(
* "/edit/{_locale}/{contentTypeSlug}/{slugOrId}",
* name="bolt_edit_content_slug",
* requirements={"contentTypeSlug"="%bolt.requirement.contenttypes%"},
* methods={"GET"})
* @Route(
* "/edit/{contentTypeSlug}/{slugOrId}",
* name="bolt_edit_content_slug",
* requirements={"contentTypeSlug"="%bolt.requirement.contenttypes%"},
* methods={"GET"})
* @see DynamicRouteLoader for content type based routes to this method.
*
* @Route(
* "/edit/{slugOrId}",
* name="bolt_edit_content_slug",
* requirements={"contentTypeSlug"="%bolt.requirement.contenttypes%"},
* methods={"GET"})
* @Route(
* "/edit/{_locale}/{slugOrId}",
* name="bolt_edit_content_slug",
* requirements={"contentTypeSlug"="%bolt.requirement.contenttypes%"},
* methods={"GET"})
*/
public function editFromSlug(?string $contentTypeSlug = null, $slugOrId): Response
Expand Down
12 changes: 2 additions & 10 deletions src/Controller/Frontend/DetailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Bolt\Configuration\Content\ContentType;
use Bolt\Controller\TwigAwareController;
use Bolt\Repository\ContentRepository;
use Bolt\Routing\DynamicRouteLoader;
use Bolt\Utils\ContentHelper;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
Expand All @@ -26,16 +27,7 @@ public function __construct(ContentRepository $contentRepository, ContentHelper
}

/**
* @Route(
* "/{contentTypeSlug}/{slugOrId}",
* name="record",
* requirements={"contentTypeSlug"="%bolt.requirement.contenttypes%"},
* methods={"GET|POST"})
* @Route(
* "/{_locale}/{contentTypeSlug}/{slugOrId}",
* name="record_locale",
* requirements={"contentTypeSlug"="%bolt.requirement.contenttypes%", "_locale": "%app_locales%"},
* methods={"GET|POST"})
* @see DynamicRouteLoader for routes to this method.
*
* @param string|int $slugOrId
*/
Expand Down
12 changes: 2 additions & 10 deletions src/Controller/Frontend/ListingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Bolt\Controller\TwigAwareController;
use Bolt\Entity\Content;
use Bolt\Repository\ContentRepository;
use Bolt\Routing\DynamicRouteLoader;
use Bolt\Storage\Query;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Pagerfanta;
Expand All @@ -28,16 +29,7 @@ public function __construct(Query $query)
}

/**
* @Route(
* "/{contentTypeSlug}",
* name="listing",
* requirements={"contentTypeSlug"="%bolt.requirement.contenttypes%"},
* methods={"GET|POST"})
* @Route(
* "/{_locale}/{contentTypeSlug}",
* name="listing_locale",
* requirements={"contentTypeSlug"="%bolt.requirement.contenttypes%", "_locale": "%app_locales%"},
* methods={"GET|POST"})
* @see DynamicRouteLoader for routes to this method.
*/
public function listing(ContentRepository $contentRepository, string $contentTypeSlug, ?string $_locale = null): Response
{
Expand Down
14 changes: 2 additions & 12 deletions src/Controller/Frontend/TaxonomyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,14 @@
use Bolt\Controller\TwigAwareController;
use Bolt\Entity\Content;
use Bolt\Repository\ContentRepository;
use Bolt\Routing\DynamicRouteLoader;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class TaxonomyController extends TwigAwareController implements FrontendZoneInterface
{
/**
* @Route(
* "/{taxonomyslug}/{slug}",
* name="taxonomy",
* requirements={"taxonomyslug"="%bolt.requirement.taxonomies%"},
* methods={"GET|POST"}
* )
* @Route(
* "/{_locale}/{taxonomyslug}/{slug}",
* name="taxonomy_locale",
* requirements={"taxonomyslug"="%bolt.requirement.taxonomies%", "_locale": "%app_locales%"},
* methods={"GET|POST"}
* )
* @see DynamicRouteLoader for routes to this method.
*/
public function listing(ContentRepository $contentRepository, string $taxonomyslug, string $slug): Response
{
Expand Down
44 changes: 1 addition & 43 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Bolt;

use Bolt\Configuration\Parser\ContentTypesParser;
use Bolt\Configuration\Parser\TaxonomyParser;
use Bolt\Extension\ExtensionCompilerPass;
use Bolt\Extension\ExtensionInterface;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
Expand All @@ -17,7 +15,6 @@
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Component\Yaml\Yaml;
use Tightenco\Collect\Support\Collection;

class Kernel extends BaseKernel
{
Expand Down Expand Up @@ -74,8 +71,7 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa
$loader->load($confDir . '/{services}_' . $this->environment . self::CONFIG_EXTS, 'glob');

$this->setBoltParameters($container, $confDir);
$this->setContentTypeRequirements($container);
$this->setTaxonomyRequirements($container);
$container->setParameter('bolt.requirement.contenttypes', "Dummy value so Checks.php in bobdenotter/configuration-notices doesn't break");
}

protected function configureRoutes(RoutingConfigurator $routes): void
Expand Down Expand Up @@ -121,44 +117,6 @@ private function flattenKeys(array $array, string $prefix = ''): array
return $result;
}

/**
* Set the ContentType requirements that are used in Routing.
* Note: this functionality is partially duplicated in \Bolt\Configuration\Config.
*
* @throws \Exception
*/
private function setContentTypeRequirements(ContainerBuilder $container): void
{
/** @var string $defaultLocale */
$defaultLocale = $container->getParameter('locale');
$ContentTypesParser = new ContentTypesParser($this->getProjectDir(), new Collection(), $defaultLocale);
$contentTypes = $ContentTypesParser->parse();

$pluralslugs = $contentTypes->pluck('slug')->implode('|');
$slugs = $contentTypes->pluck('slug')->concat($contentTypes->pluck('singular_slug'))->unique()->implode('|');

$container->setParameter('bolt.requirement.pluralcontenttypes', $pluralslugs);
$container->setParameter('bolt.requirement.contenttypes', $slugs);
}

/**
* Set the Taxonomy requirements that are used in Routing.
* Note: this functionality is partially duplicated in \Bolt\Configuration\Config.
*
* @throws \Exception
*/
private function setTaxonomyRequirements(ContainerBuilder $container): void
{
$taxonomyParser = new TaxonomyParser($this->getProjectDir());
$taxonomies = $taxonomyParser->parse();

$pluralslugs = $taxonomies->pluck('slug')->implode('|');
$slugs = $taxonomies->pluck('slug')->concat($taxonomies->pluck('singular_slug'))->unique()->implode('|');

$container->setParameter('bolt.requirement.pluraltaxonomies', $pluralslugs);
$container->setParameter('bolt.requirement.taxonomies', $slugs);
}

/**
* Return the public folder of this project. This implementation locates the public folder
* for this project by checking for the following candidates in the project dir: 'public',
Expand Down
Loading