PHP code example of musti / rate-policy

1. Go to this page and download the library: Download musti/rate-policy library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

musti / rate-policy example snippets


use Musti\RatePolicy\Traits\RateLimitRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests, RateLimitRequests;
}


use Musti\RatePolicy\RatePolicy;

class ChannelController extends Controller
{
    public function __construct(Request $request)
    {
        $this->applyRatePolicy(ChannelRatePolicy::class);
    }
}


namespace App\RatePolicies;

use App\Http\Controllers\ChannelController;
use Musti\RatePolicy\RateLimits;

class ChannelRatePolicy extends RateLimits
{
    protected $controller = ChannelController::class;

    protected function message() {
        //Do something

        return response()->json([
            'message' => 'Too many requests',
        ], 429);
    }
}


protected $maxAttempts = 10;

    protected $rateLimitForMethods = [
        'index' => 15, //index method is automatically translated to viewAny
        'store' => 5,
    ];