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

Framework Bundle Laravel Package

symfony/framework-bundle

Symfony FrameworkBundle tightly integrates Symfony components into the full-stack framework, providing core framework services and configuration. Part of the main Symfony repository; see official docs for contributing, issues, and pull requests.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require symfony/framework-bundle
    

    This package is typically included automatically when using symfony/symfony or symfony/flex.

  2. First Use Case:

    • Bootstrapping the Framework: The bundle is auto-registered in Symfony’s kernel. No manual configuration is required for basic usage.
    • Dependency Injection: Leverage Symfony’s container to autowire services:
      use Symfony\Component\HttpKernel\KernelInterface;
      
      public function __construct(private KernelInterface $kernel) {}
      
    • Routing: Define routes in config/routes.yaml or via annotations/attributes:
      # config/routes.yaml
      app_homepage:
          path: /
          controller: App\Controller\HomeController::index
      
  3. Where to Look First:

    • Documentation: Symfony FrameworkBundle Docs
    • Configuration: config/packages/framework.yaml (auto-generated by Symfony Flex).
    • Kernel: config/bundles.php (auto-configured).

Implementation Patterns

Core Workflows

  1. Service Configuration:

    • Use framework.yaml to configure core features like session, cache, or HTTP cache:
      # config/packages/framework.yaml
      framework:
          session:
              handler_id: cache.app
          http_cache:
              invalidation:
                  enabled: true
      
    • Environment-Specific Configs: Override settings per environment (e.g., config/packages/dev/framework.yaml).
  2. Dependency Injection:

    • Autowiring: Annotate constructors with types (e.g., RequestStack, RouterInterface).
    • Tagging Services: Use tags for dynamic service binding (e.g., kernel.event_listener):
      #[Tag('kernel.event_listener', ['event' => 'kernel.request'])]
      class MyListener {}
      
  3. Routing and Controllers:

    • Attribute Routing (Symfony 5.3+):
      #[Route('/profile', name: 'app_profile')]
      public function profile(): Response {}
      
    • Legacy Annotations: Migrate from @Route to attributes using useAttributeAsKey in framework.yaml:
      framework:
          router:
              use_attribute_as_key: true
      
  4. Testing:

    • Kernel Tests: Extend Symfony\Bundle\FrameworkBundle\Test\KernelTestCase:
      use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
      
      class MyTest extends KernelTestCase {
          public function testSomething(): void {
              self::bootKernel();
              $client = static::createClient();
              $client->request('GET', '/');
          }
      }
      
    • Mocking Services: Use self::$container to access services in tests.
  5. Event Listeners/Subscribers:

    • Register listeners in services.yaml:
      services:
          App\EventListener\MyListener:
              tags: ['kernel.event_listener']
      
    • Subscribers:
      tags: ['kernel.event_subscriber']
      
  6. Console Commands:

    • Create commands in src/Command/ and tag them:
      #[AsCommand(name: 'app:greet')]
      class GreetCommand extends Command {}
      
    • Register in services.yaml:
      tags: ['console.command']
      
  7. Configuration Reference:

    • Generate a reference config shape:
      php bin/console config:dump-reference framework
      

Integration Tips

  • Bundle Integration: Use BundleInterface to create custom bundles with FrameworkBundle features.
  • Environment Variables: Access via %env(APP_SECRET)% in framework.yaml or .env files.
  • Debugging: Enable debug mode in .env (APP_DEBUG=1) and use the profiler (/_profiler).

Gotchas and Tips

Pitfalls

  1. Caching Quirks:

    • Cache Warmup: Run php bin/console cache:warmup after config changes. In tests, use self::bootKernel() to ensure a fresh container.
    • Stale Containers: Avoid reusing the same kernel instance across tests (fixed in Symfony 6.4+ via KernelTestCase).
  2. Routing Issues:

    • Attribute vs. Annotation: Ensure use_attribute_as_key: true is set for attribute routing to work.
    • Route Overrides: Prioritize YAML routes over annotations/attributes if conflicts arise.
  3. Dependency Injection:

    • Circular Dependencies: Use autowire: false and explicit binding if needed.
    • Service Not Found: Verify the service is tagged or registered in services.yaml.
  4. Session Handling:

    • Mock Sessions: Use MockSessionStorage in tests:
      $session = new Session();
      $session->setStorage(new MockSessionStorage());
      self::$container->set('session', $session);
      
    • Cookie Lifetime: Ensure cookie_lifetime is set in framework.yaml for session cookies.
  5. Configuration Merging:

    • Duplicate Keys: Symfony merges configs left-to-right. Override defaults in config/packages/overrides.yaml.
    • Reference Config: Use config:dump-reference to debug config shapes.
  6. Testing Gotchas:

    • HttpCache: Clear cache in KernelTestCase:
      self::ensureKernelShutdown();
      
    • Decorated Services: Mock decorated services explicitly in tests.
  7. Console Commands:

    • Missing Console Package: Ensure symfony/console is installed if using commands.
    • Command Data Collector: Only enabled if the console package is present.

Debugging Tips

  1. Dump Container Services:
    php bin/console debug:container
    
  2. Check Routes:
    php bin/console debug:router
    
  3. Environment Variables:
    php bin/console debug:env
    
  4. Configuration:
    php bin/console debug:config framework
    
  5. Event Dispatcher:
    php bin/console debug:event-dispatcher
    

Extension Points

  1. Custom Configurators:
    • Implement ConfiguratorInterface for custom config validation/normalization.
  2. Event Subscribers:
    • Extend EventSubscriberInterface for kernel events (e.g., kernel.request).
  3. HttpKernel Extensions:
    • Override HttpKernelInterface for custom request/response handling.
  4. Service Providers:
    • Use CompilerPass to modify the container at compile time.
  5. Routing Loaders:
    • Create custom LoaderInterface implementations for dynamic routes.

Performance Tips

  • Cache Pools: Use cache.app for session storage to reduce database load.
  • HTTP Cache: Enable http_cache for static assets:
    framework:
        http_cache:
            enabled: true
    
  • OpCache: Ensure OPcache is enabled for PHP bytecode caching.

Security Tips

  • CSRF Protection: Enable in framework.yaml:
    framework:
        csrf_protection:
            enabled: true
    
  • HSTS: Configure in framework.yaml:
    framework:
        http_client:
            headers:
                Strict-Transport-Security: "max-age=31536000; includeSubDomains"
    
  • CORS: Use nelmio/cors-bundle for cross-origin requests.
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.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi