contextualcode/ezplatform-admin-url-alias-redirects
Installation
Add the bundle to your composer.json:
composer require contextualcode/ezplatform-admin-url-alias-redirects
Enable the bundle in config/bundles.php:
return [
// ...
ContextualCode\EzPlatformAdminUrlAliasRedirects\EzPlatformAdminUrlAliasRedirectsBundle::class => ['all' => true],
];
Configuration
Ensure your ezplatform.yml includes the admin siteaccess configuration:
system:
siteaccess:
admin:
# ...
First Use Case
Navigate to an admin URL (e.g., /admin) while logged in. The bundle will automatically redirect front-end URL aliases (e.g., /product/123) to their corresponding admin locations (e.g., /admin/content/edit/123).
Front-End to Admin Redirects
/product/123) while in an admin siteaccess, the bundle intercepts the request and redirects to the admin content edit page (/admin/content/edit/123).Integration with eZ Platform
UrlAliasService and ContentService to resolve front-end aliases to admin content IDs.UrlAliasRedirectListener to customize redirect logic (e.g., exclude specific content types):
// src/EventListener/CustomUrlAliasRedirectListener.php
namespace App\EventListener;
use ContextualCode\EzPlatformAdminUrlAliasRedirects\Event\UrlAliasRedirectEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CustomUrlAliasRedirectListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
UrlAliasRedirectEvent::NAME => 'onUrlAliasRedirect',
];
}
public function onUrlAliasRedirect(UrlAliasRedirectEvent $event)
{
if ($event->getContent()->getContentInfo()->contentTypeId === 123) {
$event->setRedirect(false); // Skip redirect for this content type
}
}
}
Admin URL Generation
Use the AdminUrlGenerator service to generate admin URLs programmatically:
$adminUrl = $this->container->get('ezplatform.admin_url_generator')->generate(
'content/edit',
['contentId' => 123]
);
Testing
UrlAliasRedirectListener in PHPUnit tests to verify redirects:
$event = new UrlAliasRedirectEvent($request, $content, $urlAlias);
$listener->onUrlAliasRedirect($event);
$this->assertTrue($event->isRedirect());
Caching Issues
php bin/console cache:clear
UrlAliasService cache is populated.Siteaccess Mismatch
admin siteaccess. Ensure your ezplatform.yml correctly defines the admin siteaccess:
system:
siteaccess:
admin:
base_url: /admin
Content Not Found
NotFoundHttpException. Handle this gracefully in your UrlAliasRedirectListener:
public function onUrlAliasRedirect(UrlAliasRedirectEvent $event)
{
if (!$event->getContent()) {
$event->setRedirect(false);
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException('Content not found');
}
}
Performance Overhead
Enable Debugging
Set debug: true in config/packages/dev/ezplatform_admin_url_alias_redirects.yaml:
ezplatform_admin_url_alias_redirects:
debug: true
Logs redirect attempts to var/log/dev.log.
Check Redirect Logic
Override the UrlAliasRedirectListener to log resolved content:
public function onUrlAliasRedirect(UrlAliasRedirectEvent $event)
{
$this->logger->info('Redirecting alias: ' . $event->getUrlAlias()->getAlias(), ['contentId' => $event->getContent()->getContentInfo()->id]);
}
Exclude Specific Routes Skip redirects for API or non-content routes by extending the listener:
public function onUrlAliasRedirect(UrlAliasRedirectEvent $event)
{
if (str_starts_with($event->getRequest()->getPathInfo(), '/api')) {
$event->setRedirect(false);
}
}
Custom Redirect Routes
Extend the AdminUrlGenerator to support custom admin routes:
// src/AdminUrlGeneratorExtension.php
namespace App\AdminUrlGenerator;
use ContextualCode\EzPlatformAdminUrlAliasRedirects\AdminUrlGenerator\AdminUrlGeneratorInterface;
class AdminUrlGeneratorExtension implements AdminUrlGeneratorInterface
{
public function generate(string $route, array $parameters): string
{
// Custom logic for non-standard routes
return '/custom-admin-path';
}
}
Register the extension in services.yaml:
services:
App\AdminUrlGenerator\AdminUrlGeneratorExtension:
tags:
- { name: 'ezplatform.admin_url_generator.extension' }
Dynamic Siteaccess Handling Dynamically determine the admin siteaccess based on user roles:
public function onUrlAliasRedirect(UrlAliasRedirectEvent $event)
{
if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
$event->setAdminSiteaccess('super_admin');
}
}
How can I help you explore Laravel packages today?