Installation:
composer require bluebear/basebundle
Add to config/bundles.php:
return [
// ...
BlueBear\BaseBundle\BlueBearBaseBundle::class => ['all' => true],
];
First Use Case:
Extend a controller with ControllerTrait to leverage shortcuts:
use BlueBear\BaseBundle\Behavior\ControllerTrait;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class HomeController extends AbstractController {
use ControllerTrait;
public function index() {
$this->setMessage('Welcome!', 'success');
return $this->render('home/index.html.twig');
}
}
Key Entry Points:
setMessage() for user notifications.__forward404Unless() for conditional 404s.__getRouting(), __getTranslator(), etc., for direct service access.Flash Messages:
// In controller
$this->setMessage('Action completed', 'success', ['%action%' => 'Update']);
// In Twig: {{ app.flashes('success')|first }}
Conditional 404s:
public function show($id) {
$this->__forward404Unless($this->getEntityManager()->find('App\Entity\Post', $id));
// Rest of logic...
}
Route Generation:
$url = $this->generateUrl('app_home');
$this->redirect($url); // or $this->redirect('@app_home')
Configuration Access:
$apiKey = $this->__getConfig('app.api_key');
Service Shortcuts:
$translator = $this->__getTranslator();
$session = $this->__getSession();
Replace Native Methods:
Override Symfony’s native methods (e.g., redirect()) with ControllerTrait versions for consistency.
// Instead of:
return $this->redirectToRoute('home');
// Use:
return $this->redirect('@home');
Twig Extensions: Extend Twig to use flash messages or other traits:
{% if app.flashes('error')|length %}
<div class="alert alert-error">{{ app.flashes('error')|first }}</div>
{% endif %}
Event Dispatching:
Use __getEventDispatcher() to dispatch events:
$this->__getEventDispatcher()->dispatch(new PostEvent($post), 'post.create');
Deprecation Risk:
Trait Overrides:
ControllerTrait overrides native methods (e.g., redirect()). Ensure no naming conflicts with custom methods._customRedirect()).Configuration Access:
__getConfig() fetches from parameters in config/packages/devries_base.yaml (Symfony 2.x style).container.getParameter() for clarity in modern apps.Session/Translator:
public function __construct(
private RouterInterface $router,
private TranslatorInterface $translator
) {}
404 Logic:
__forward404Unless() throws a NotFoundHttpException. Ensure your ExceptionListener (Symfony 2.x) handles it.Flash Messages Not Showing:
Verify Twig extension is registered (Symfony 2.x twig.extension config).
# config/packages/twig.yaml
twig:
extensions:
- BlueBear\BaseBundle\Twig\BaseExtension
Route Generation Errors:
Use absolute routes (@route_name) with redirect() to avoid hardcoded URLs.
Service Unavailability:
Check if the bundle is properly enabled in bundles.php (Symfony 4+).
Customize Flash Types:
Extend BaseExtension (if available) to add new flash message types:
// src/Twig/Extension/CustomBaseExtension.php
class CustomBaseExtension extends BaseExtension {
public function getFlashTypes() {
return array_merge(parent::getFlashTypes(), ['warning']);
}
}
Override Trait Methods: Create a custom trait to extend functionality:
trait CustomControllerTrait {
public function logAction($message) {
$this->__getLogger()->info($message);
}
}
Add New Services: Bind additional services to the trait via dependency injection:
// src/Behavior/CustomControllerTrait.php
use Psr\Log\LoggerInterface;
trait CustomControllerTrait {
public function __construct(private LoggerInterface $logger) {}
public function log($message) { $this->logger->info($message); }
}
How can I help you explore Laravel packages today?