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

Basebundle Laravel Package

bluebear/basebundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require bluebear/basebundle
    

    Add to config/bundles.php:

    return [
        // ...
        BlueBear\BaseBundle\BlueBearBaseBundle::class => ['all' => true],
    ];
    
  2. 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');
        }
    }
    
  3. Key Entry Points:

    • Flash Messages: Use setMessage() for user notifications.
    • 404 Handling: Use __forward404Unless() for conditional 404s.
    • Service Access: Use __getRouting(), __getTranslator(), etc., for direct service access.

Implementation Patterns

Common Workflows

  1. Flash Messages:

    // In controller
    $this->setMessage('Action completed', 'success', ['%action%' => 'Update']);
    // In Twig: {{ app.flashes('success')|first }}
    
  2. Conditional 404s:

    public function show($id) {
        $this->__forward404Unless($this->getEntityManager()->find('App\Entity\Post', $id));
        // Rest of logic...
    }
    
  3. Route Generation:

    $url = $this->generateUrl('app_home');
    $this->redirect($url); // or $this->redirect('@app_home')
    
  4. Configuration Access:

    $apiKey = $this->__getConfig('app.api_key');
    
  5. Service Shortcuts:

    $translator = $this->__getTranslator();
    $session = $this->__getSession();
    

Integration Tips

  • 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');
    

Gotchas and Tips

Pitfalls

  1. Deprecation Risk:

    • Last updated in 2015; may conflict with modern Symfony (5.4+/Laravel-like workflows).
    • Avoid in new projects unless maintaining legacy code.
  2. Trait Overrides:

    • ControllerTrait overrides native methods (e.g., redirect()). Ensure no naming conflicts with custom methods.
    • Fix: Prefix custom methods (e.g., _customRedirect()).
  3. Configuration Access:

    • __getConfig() fetches from parameters in config/packages/devries_base.yaml (Symfony 2.x style).
    • Tip: Use Symfony’s container.getParameter() for clarity in modern apps.
  4. Session/Translator:

    • Direct service access bypasses dependency injection. Prefer constructor injection for testability:
      public function __construct(
          private RouterInterface $router,
          private TranslatorInterface $translator
      ) {}
      
  5. 404 Logic:

    • __forward404Unless() throws a NotFoundHttpException. Ensure your ExceptionListener (Symfony 2.x) handles it.

Debugging Tips

  • 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+).

Extension Points

  1. 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']);
        }
    }
    
  2. Override Trait Methods: Create a custom trait to extend functionality:

    trait CustomControllerTrait {
        public function logAction($message) {
            $this->__getLogger()->info($message);
        }
    }
    
  3. 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); }
    }
    
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.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware