Installation
composer require apb/hello-world-bundle
Ensure autoload is dumped:
composer dump-autoload
Enable the Bundle
Add to config/bundles.php:
return [
// ...
Apb\HelloWorldBundle\ApbHelloWorldBundle::class => ['all' => true],
];
First Use Case Inject the service into a controller or template:
use Apb\HelloWorldBundle\Service\HelloWorldService;
class TestController extends AbstractController
{
public function index(HelloWorldService $helloWorld): Response
{
return $this->render('test/index.html.twig', [
'message' => $helloWorld->getMessage(),
]);
}
}
In Twig:
{{ message }} {# Renders "Hello World" #}
Dependency Injection: Use HelloWorldService in controllers, commands, or other services.
public function __construct(private HelloWorldService $helloWorld) {}
Configuration Overrides
Override the default message via config/packages/apb_hello_world.yaml:
message: "Custom Greeting"
Twig Extension Access the message directly in templates:
{{ app.service('apb_hello_world.hello_world').getMessage() }}
Dynamic Messages Extend the service to accept parameters:
$helloWorld->getMessage('User123'); // "Hello User123"
Localization Use Symfony’s translation system to localize the message:
{{ 'apb_hello_world.message'|trans }}
Define translations in translations/messages.en.yaml:
apb_hello_world:
message: "Bonjour le monde" # French example
Event Listeners Trigger actions when the message is rendered (e.g., logging):
use Apb\HelloWorldBundle\Event\MessageRenderedEvent;
public function onMessageRendered(MessageRenderedEvent $event)
{
// Log or process the rendered message
}
Register in services.yaml:
services:
App\Listener\HelloWorldListener:
tags:
- { name: 'kernel.event_listener', event: 'apb_hello_world.message_rendered' }
Bundle Not Enabled
ServiceNotFoundException for HelloWorldService.ApbHelloWorldBundle is listed in config/bundles.php.Caching Issues
config/packages/apb_hello_world.yaml not reflected.php bin/console cache:clear
Namespace Conflicts
Apb\HelloWorldBundle.\Apb\HelloWorldBundle\Service\HelloWorldService).Check Service Availability Run:
php bin/console debug:container apb_hello_world.hello_world
Should return:
Service "apb_hello_world.hello_world" is available.
Log Service Calls
Temporarily modify HelloWorldService to log invocations:
public function getMessage(?string $name = null): string
{
\Log::debug('HelloWorldService called with name:', ['name' => $name]);
return $name ? "Hello $name" : 'Hello World';
}
Customize the Service
Override the service definition in config/services.yaml:
services:
Apb\HelloWorldBundle\Service\HelloWorldService:
arguments:
$defaultMessage: "Overridden Message"
Add New Features
Extend the service class (e.g., App\Service\ExtendedHelloWorldService) and replace it in services.yaml:
services:
apb_hello_world.hello_world:
class: App\Service\ExtendedHelloWorldService
arguments: ['@apb_hello_world.hello_world.inner']
Twig Filters Create a custom Twig filter for the message:
// src/Twig/AppExtension.php
public function getFilters()
{
return [
new \Twig\TwigFilter('hello_world', [$this->helloWorld, 'getMessage']),
];
}
Usage in Twig:
{{ 'Alice'|hello_world }} {# "Hello Alice" #}
Default Values
The bundle uses Hello World as the default message. To change it globally:
# config/packages/apb_hello_world.yaml
message: "Default App Greeting"
Environment-Specific Config
Use %kernel.environment% to load different messages per environment:
# config/packages/apb_hello_world.yaml
message: "%env(HELLO_WORLD_MESSAGE)%"
Set the env variable:
export HELLO_WORLD_MESSAGE="Dev Environment Greeting"
How can I help you explore Laravel packages today?