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

Traffic Limit Bundle Laravel Package

devoralive/traffic-limit-bundle

Symfony bundle for rate limiting requests via Redis using SncRedisBundle. Define multiple limit profiles (amount/ttl) and Redis clients in config, then access the generated services from the container to enforce per-key traffic limits.

View on GitHub
Deep Wiki
Context7

TrafficLimitBundle

Limit the amount of request to your application based on a defined key

It uses SNC\RedisBundle to connect to redis. You can create as many traffic limit services as you require

Installation

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require devoralive/traffic-limit-bundle "dev-master"

This will also install snc\RedisBundle if you did not have it installed

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php file of your project:

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Snc\RedisBundle\SncRedisBundle(), //Mandatory of using this bundle
            new Devoralive\TrafficLimit\TrafficLimitBundle(), //Include the bundle
        );

        // ...
    }

    // ...
}

Step 3: Configure the bundle

To use this bundle you need to add configuration into the config.yml file inside you app/config directory

We supose you al ready included at least one connection to redis from the sncRedisBundle If you need more information have a look at SncRedisBundle configuration

snc_redis:
    clients:
        default:
            type: phpredis
            alias: default
            dsn: redis://localhost:6379

        traffic_limit:
            type: phpredis
            alias: default
            dsn: redis://localhost:6379
            
traffic_limit:
    low_limit:
        enabled: true
        snc_client: traffic_limit
        amount: 600
        ttl: 60
    high_limit:
        enabled: true
        snc_client: default
        amount: 6000
        ttl: 60

As you can see you can define as many traffic limit services as you desire. You can have infinite configurations. All are available in the container:

<?php

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;

namespace AppBundle\Controller;

/**
 * Class ApiController
 *
 * @package AppBundle\Controller
 */
class ApiController
{
    /**
     * Example on how to limit requests by IP
     *     
     * @param string $ip
     *
     * @return string 
     *
     * @throws TooManyRequestsHttpException $e
     */
    public function getLocationAction(Request $request, $ip)
    {
        try {
            $this->get('traffic_limit.low_limit')->processRequest(
                $request->getClientIp()
            );
                    
            //do some stuff...
            
            return new JsonResponse('');
        } catch (TooManyRequestsHttpException $e) {
            //handle exception or throw it
        }
    }
}

You can define the key, so you can limit the request by IP, userId, or anything you can identify as a variable.

If the limit of requests is reached the method "processRequest" will return a exception.

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.
babenkoivan/elastic-client
innmind/static-analysis
innmind/coding-standard
datacore/hub-sdk
alengo/sulu-http-cache-bundle
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
imbo/imbo-coding-standard
visualbuilder/filament-lottie
servicioslineaonce/starter-kit
atomcoder/laravel-reorderable
irajul/filament-shadcn-theme
agtp/agtp-php
agtp/mod-php
centraldesktop/protobuf-php