Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Ezplatform Admin Url Alias Redirects Laravel Package

contextualcode/ezplatform-admin-url-alias-redirects

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps to Begin

  1. 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],
    ];
    
  2. Configuration Ensure your ezplatform.yml includes the admin siteaccess configuration:

    system:
        siteaccess:
            admin:
                # ...
    
  3. 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).


Implementation Patterns

Core Workflow

  1. Front-End to Admin Redirects

    • When a user accesses a front-end URL (e.g., /product/123) while in an admin siteaccess, the bundle intercepts the request and redirects to the admin content edit page (/admin/content/edit/123).
    • Example: A developer testing content in admin mode can directly access front-end URLs without manually navigating to admin paths.
  2. Integration with eZ Platform

    • The bundle hooks into eZ Platform’s UrlAliasService and ContentService to resolve front-end aliases to admin content IDs.
    • Extend the 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
              }
          }
      }
      
  3. 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]
    );
    
  4. Testing

    • Mock the UrlAliasRedirectListener in PHPUnit tests to verify redirects:
      $event = new UrlAliasRedirectEvent($request, $content, $urlAlias);
      $listener->onUrlAliasRedirect($event);
      $this->assertTrue($event->isRedirect());
      

Gotchas and Tips

Pitfalls

  1. Caching Issues

    • Clear the Symfony cache after installing/updating the bundle:
      php bin/console cache:clear
      
    • If redirects fail, verify the UrlAliasService cache is populated.
  2. Siteaccess Mismatch

    • The bundle only works in the admin siteaccess. Ensure your ezplatform.yml correctly defines the admin siteaccess:
      system:
          siteaccess:
              admin:
                  base_url: /admin
      
  3. Content Not Found

    • If a front-end URL alias resolves to a non-existent content ID, the bundle throws a 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');
          }
      }
      
  4. Performance Overhead

    • The bundle adds a small overhead to front-end requests in admin mode. Test performance in staging with realistic traffic.

Debugging Tips

  1. 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.

  2. 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]);
    }
    
  3. 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);
        }
    }
    

Extension Points

  1. 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' }
    
  2. 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');
        }
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament