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

Ddd Sylius Bundle Laravel Package

alexandrebulete/ddd-sylius-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the Bundle:
    composer require alexandrebulete/ddd-sylius-bundle
    
  2. Register the Bundle in config/bundles.php:
    AlexandreBulete\DddSyliusBundle\DddSyliusBundle::class => ['all' => true],
    
  3. Configure Sylius Resource Mapping in config/packages/sylius_resource.yaml:
    sylius_resource:
        mapping:
            paths:
                - '%kernel.project_dir%/src/*/Infrastructure/Sylius/Resource'
    

First Use Case: Adding a Menu Item

Create a PostMenuContributor in your bounded context (e.g., src/Post/Infrastructure/Symfony/Menu/PostMenuContributor.php):

use AlexandreBulete\DddSyliusBundle\Admin\Menu\MenuContributorInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

#[AutoconfigureTag('app.menu_contributor')]
final readonly class PostMenuContributor implements MenuContributorInterface
{
    public function contribute(ItemInterface $menu): void
    {
        $menu
            ->addChild('posts', ['route' => 'app_admin_post_index'])
            ->setLabel('Posts')
            ->setLabelAttribute('icon', 'tabler:article');
    }
}

The menu item will now appear in the Sylius Admin UI under the configured route.


Implementation Patterns

Modular Menu Contribution

  • Workflow: Each bounded context (e.g., Post, User) defines its own menu contributor.
  • Integration: Tag contributors with #[AutoconfigureTag('app.menu_contributor')] to auto-register them.
  • Best Practice: Group contributors in src/{Context}/Infrastructure/Symfony/Menu/ for clarity.

Grid Integration

  • Workflow:
    1. Define a Grid class (e.g., PostGrid) in src/{Context}/Infrastructure/Sylius/Grid/.
    2. Use GridPageResolver to handle pagination:
      $page = GridPageResolver::getCurrentPage($grid, $request->query->all());
      $itemsPerPage = GridPageResolver::getItemsPerPage($grid, $request->query->all());
      
  • Tip: Extend Sylius\Grid\GridInterface for custom grid logic.

Resource Mapping

  • Workflow:
    1. Define a Resource class (e.g., PostResource) in src/{Context}/Infrastructure/Sylius/Resource/.
    2. Annotate it with Sylius resource metadata (e.g., @Sylius\Resource\Resource).
  • Example:
    use Sylius\ResourceBundle\Doctrine\ORM\EntityRepository;
    use Sylius\ResourceBundle\Resource\ResourceInterface;
    
    #[Sylius\Resource\Resource('post')]
    class Post implements ResourceInterface
    {
        use Sylius\ResourceBundle\Doctrine\ORM\EntityTrait;
    }
    

Bounded Context Structure

  • Recommended Layout:
    src/
    ├── Post/
    │   ├── Domain/          # Core logic (entities, services)
    │   ├── Infrastructure/
    │   │   ├── Sylius/      # Sylius-specific integrations (Grid, Resource)
    │   │   └── Symfony/     # Symfony-specific integrations (Menu, Controllers)
    │   └── Application/     # Use cases, commands
    

Gotchas and Tips

Pitfalls

  1. Menu Contributor Tagging:

    • Forgetting #[AutoconfigureTag('app.menu_contributor')] will prevent the menu item from appearing.
    • Fix: Verify the tag is applied and the service is autowired.
  2. Grid Pagination Issues:

    • If maxPerPage is not updating, ensure you’re using GridPageResolver correctly. The bundle fixes this in 1.0.1 via RequestGrid decorator.
    • Debug: Check if pagerfanta.max_per_page is being overridden elsewhere.
  3. Resource Mapping Paths:

    • Glob paths (%kernel.project_dir%/src/*/Infrastructure/Sylius/Resource) must match your project structure. Misconfiguration will ignore custom resources.
    • Fix: Explicitly list paths if globbing fails:
      sylius_resource:
          mapping:
              paths:
                  - '%kernel.project_dir%/src/Post/Infrastructure/Sylius/Resource'
      

Debugging Tips

  • Menu Not Showing?:
    • Clear the cache (php bin/console cache:clear).
    • Check for Symfony events that might alter the menu (e.g., knp_menu.menu).
  • Grid Data Missing:
    • Verify the Grid class is properly bound to a repository and the entity is mapped in Doctrine.
    • Use dd($grid->getQuery()->getResult()) to inspect raw data.

Extension Points

  1. Custom Menu Items:
    • Extend MenuContributorInterface to add dynamic logic (e.g., role-based visibility):
      public function contribute(ItemInterface $menu): void {
          if ($this->security->isGranted('ROLE_ADMIN')) {
              $menu->addChild('admin_posts', [...]);
          }
      }
      
  2. Grid Filters:
    • Override Grid methods like configure() to add custom filters:
      public function configure(): void {
          $this->add('status', 'status', [/* ... */]);
      }
      
  3. Resource Events:
    • Listen to Sylius resource events (e.g., prePersist) in your bounded context’s infrastructure:
      use Sylius\Component\Resource\Event\ResourceEvent;
      
      $dispatcher->addListener(
          'sylius.resource.post.pre_persist',
          fn(ResourceEvent $event) => $this->prePersistLogic($event->getSubject())
      );
      

Configuration Quirks

  • Sylius Version Compatibility:
    • The bundle targets Sylius 1.11 for resource-bundle and 1.13 for grid-bundle. Mismatches may cause errors.
    • Fix: Align Sylius versions in composer.json or pin the bundle version.
  • Admin UI Theme:
    • Icons (e.g., tabler:article) rely on the active Sylius admin theme (e.g., Bootstrap). Test icons in your theme’s documentation.
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.
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
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