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

Core Bundle Laravel Package

sonata-project/core-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require sonata-project/core-bundle
    

    Add to config/bundles.php:

    return [
        // ...
        Sonata\CoreBundle\SonataCoreBundle::class => ['all' => true],
    ];
    
  2. First Use Case:

    • Admin Generator: SonataCoreBundle is the foundation for SonataAdminBundle. If you're building an admin panel, start by configuring the sonata_admin bundle (which depends on this core bundle).
    • Basic Routing: Override default routes by extending SonataCoreBundle's routing configuration in config/routes/sonata_admin.yaml:
      sonata_admin:
          resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
          prefix: /admin
      
  3. Where to Look First:

    • Documentation: Check the SonataProject docs (archived but still useful).
    • Configuration: Key configs are in config/packages/sonata_core.yaml (auto-generated if missing).
    • Templates: Core templates are in vendor/sonata-project/core-bundle/Resources/views/.

Implementation Patterns

Common Workflows

  1. Admin Integration:

    • Use sonata.admin service to access admin services:
      $adminPool = $this->get('sonata.admin.pool');
      $admin = $adminPool->getAdminByAdminCode('YourAdminClass');
      
    • Extend admin classes by overriding configureFormFields() or configureListFields() in your custom admin classes.
  2. Asset Management:

    • SonataCoreBundle provides a sonata.block.service.block_pool for dynamic blocks. Register a block service:
      # config/services.yaml
      services:
          App\Block\MyBlockService:
              tags:
                  - { name: sonata.block }
      
  3. Security:

    • Use sonata.security.impersonating_proxy for user impersonation (if enabled). Configure in config/packages/security.yaml:
      security:
          impersonating: ~
      
  4. Event Listeners:

    • Listen to Sonata events (e.g., sonata.admin.event.configure):
      use Sonata\AdminBundle\Event\ConfigureEvent;
      
      public function onAdminConfigure(ConfigureEvent $event) {
          $event->getAdmin()->getShow()->setActions(['list', 'create']);
      }
      
      Tag your listener:
      tags:
          - { name: kernel.event_listener, event: sonata.admin.event.configure, method: onAdminConfigure }
      
  5. Translation:

    • Override translations in translations/messages.en.yaml:
      sonata_admin:
          block:
              title: "Custom Block Title"
      

Integration Tips

  • Laravel-Symfony Bridge: If using Laravel, leverage symfony/console and symfony/dependency-injection via spatie/laravel-symfony-support for Sonata integration.
  • Doctrine ORM: SonataCoreBundle assumes Doctrine ORM. Configure in config/packages/doctrine.yaml if using Laravel’s Eloquent.
  • Twig Extensions: Use Sonata’s Twig extensions (e.g., sonata_block):
    {{ render(block('block_name')) }}
    

Gotchas and Tips

Pitfalls

  1. Deprecation Warning:

  2. Symfony-Specific:

    • No Laravel Support: SonataCoreBundle is Symfony-only. For Laravel, use Laravel-specific admin packages or adapt Symfony bundles via bridges.
    • Configuration Overrides: Sonata relies heavily on YAML/XML configs. Laravel’s PHP-based configs may require manual conversion.
  3. Routing Conflicts:

    • Sonata’s default routes (e.g., /admin) may clash with Laravel’s routes. Use route prefixes or middleware to isolate:
      // routes/web.php
      Route::prefix('admin')->middleware(['web', 'auth'])->group(function () {
          // Sonata routes here
      });
      
  4. Doctrine Events:

    • Sonata triggers Doctrine lifecycle events (e.g., prePersist). Override these carefully to avoid conflicts with Laravel’s Eloquent events.
  5. Asset Pipeline:

    • Sonata uses Webpack Encore for assets. In Laravel, configure webpack.mix.js to handle Sonata’s JS/CSS:
      mix.copy('vendor/sonata-project/core-bundle/web/bundles/sonataadmin/less', 'public/assets/sonata');
      

Debugging Tips

  1. Enable Debug Mode:

    • Set SONATA_DEBUG env var or add to config/packages/sonata_core.yaml:
      sonata_core:
          debug: true
      
  2. Log Events:

    • Enable Sonata’s event logging:
      sonata_core:
          logging:
              enabled: true
              level: debug
      
  3. Common Errors:

    • "Service Not Found": Ensure the bundle is loaded in config/bundles.php and dependencies (e.g., sonata-doctrine-orm-admin) are installed.
    • Template Not Found: Clear cache (php bin/console cache:clear) and verify template paths in templates/ or vendor/.
  4. Database Migrations:

    • Sonata’s admin classes often require Doctrine entities. Run migrations after defining them:
      php bin/console doctrine:migrations:diff
      php bin/console doctrine:migrations:migrate
      

Extension Points

  1. Custom Blocks:

    • Extend Sonata\BlockBundle\Block\BlockContextAwareInterface to create reusable blocks. Example:
      class CustomBlock extends BlockBase {
          public function execute(BlockContextInterface $blockContext, Response $response) {
              return $response->setContent('Custom content');
          }
      }
      
  2. Admin Customization:

    • Override admin templates by copying from vendor/sonata-project/core-bundle/Resources/views/ to templates/SonataAdminBundle/.
    • Extend admin classes:
      class CustomPostAdmin extends SonataAdminBundleAdmin {
          protected function configureFormFields(FormMapper $formMapper) {
              $formMapper->add('title', 'text');
          }
      }
      
  3. Security Roles:

    • Define custom roles in security.yaml:
      roles:
          SonataAdminBundle:
              resource: '@SonataAdminBundle/Resources/config/security/roles.yml'
      
  4. Internationalization:

    • Override translations dynamically via event listeners or use Symfony’s translation system:
      $translator->trans('sonata_admin.block.title', [], 'SonataCoreBundle');
      
  5. Performance:

    • Disable unused features (e.g., sonata_core.debug) in production.
    • Use sonata.core.cache to optimize rendering:
      sonata_core:
          cache:
              app: cache.app
              security: cache.security
      
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
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