Weave 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('[email protected]');
    

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('[email protected]');
} 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('[email protected]');
    if (!$cache->has($key)) {
        $user = $tyrApi->getUserByEmail('[email protected]');
        $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.
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel
draftsmaninc/draftsman
square-bit/laravel-volition
himito/laravel-mailman
htinlynn/genius-helper
maco-studios/hephaestus
adachsoft/php-code-style