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

Tyr Bundle Laravel Package

canaltp/tyr-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require canaltp/tyr-bundle:1.1.x
    

    Add the bundle to AppKernel.php:

    new CanalTP\TyrBundle\CanalTPTyrBundle(),
    
  2. Configure: Update app/config.yml with your Tyr API endpoint:

    canal_tp_tyr:
        url: %tyr_url%
        end_point_id: 2  # Optional
    

    Define the tyr_url parameter in parameters.yml:

    parameters:
        tyr_url: http://tyr.dev.canaltp.fr/v0/
    
  3. First Use Case: Inject the service via dependency injection or fetch it from the container:

    $tyrApi = $this->get('canal_tp_tyr.api');
    $user = $tyrApi->getUserByEmail('user@example.com');
    

Implementation Patterns

Dependency Injection

Leverage Symfony’s DI container for cleaner code:

// In a controller/service
public function __construct(CanalTP\TyrBundle\Service\TyrApi $tyrApi) {
    $this->tyrApi = $tyrApi;
}

Dynamic Endpoint Handling

Set the endpoint ID dynamically (e.g., per request or user):

$tyrApi->setEndPointId($request->get('endpoint_id'));

Error Handling

Wrap API calls in try-catch blocks to handle exceptions gracefully:

try {
    $user = $tyrApi->getUserByEmail('user@example.com');
} catch (\Exception $e) {
    $this->addFlash('error', 'Failed to fetch user: ' . $e->getMessage());
}

Integration with Symfony Events

Use events to pre-process or post-process API responses:

// In a listener
public function onKernelRequest(GetResponseEvent $event) {
    $tyrApi = $this->container->get('canal_tp_tyr.api');
    $tyrApi->setEndPointId($this->getEndpointIdFromContext());
}

Gotchas and Tips

Configuration Quirks

  • Missing end_point_id: If not set in config.yml, ensure it’s dynamically set via setEndPointId() or the API will fail.
  • URL Trailing Slash: The url parameter in config.yml must end with / (e.g., http://tyr.dev/v0/). Omitting it may cause 404 errors.

Debugging

  • Check HTTP Client: The bundle uses Symfony’s HttpClient under the hood. Enable debug mode to inspect requests:
    # config.yml
    framework:
        http_client:
            debug: true
    
  • Log Responses: Extend AbstractTyrService to log raw responses for debugging:
    $tyrApi->getClient()->getEventDispatcher()->addListener(
        'response',
        function ($event) {
            $this->logger->debug('Tyr API Response:', ['response' => $event->getResponse()->getContent()]);
        }
    );
    

Extension Points

  • Custom Endpoints: Override the service to add custom methods:
    // src/Service/CustomTyrApi.php
    class CustomTyrApi extends \CanalTP\TyrBundle\Service\TyrApi {
        public function getCustomData($id) {
            return $this->get('http_client')->request('GET', $this->url . 'custom/' . $id);
        }
    }
    
    Register it as a service in services.yml:
    services:
        canal_tp_tyr.api:
            class: App\Service\CustomTyrApi
            arguments: ['@http_client', '%tyr_url%', '%kernel.debug%']
    

Performance

  • Caching: Cache frequent API calls using Symfony’s cache system:
    $cache = $this->container->get('cache.app');
    $key = 'tyr_user_' . md5('user@example.com');
    if (!$cache->has($key)) {
        $user = $tyrApi->getUserByEmail('user@example.com');
        $cache->set($key, $user, 3600); // Cache for 1 hour
    } else {
        $user = $cache->get($key);
    }
    

Deprecation Note

  • Last Release (2018): The package is outdated. Test thoroughly in a staging environment before production use. Consider forking or replacing with a maintained alternative (e.g., Guzzle HTTP for custom API clients).
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.
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge