PHP code example of coosos / ip-filter-bundle

1. Go to this page and download the library: Download coosos/ip-filter-bundle 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/ */

    

coosos / ip-filter-bundle example snippets



// config/bundles.php

return [
    // .....
    Coosos\IpFilterBundle\CoososIpFilterBundle::class => ['all' => true]
    // .....
];



// src/Entity/Ip.php

namespace App\Entity;

use Coosos\IpFilterBundle\Entity\Ip as BaseIp;
use Doctrine\ORM\Mapping as ORM;

/**
 * Ip
 *
 * @ORM\Table(name="ips")
 */
class Ip extends BaseIp
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function getId()
    {
        return $this->id;
    }
}



use Coosos\IpFilterBundle\Model\IpManagerInterface;

/** @var IpManagerInterface $ipManager */
$ipManager = $this->container->get('sl_ip_filter.ip_manager'); //Use this line, even if you use a custom IP manager

//Create your IP
$ip = $ipManager->createIp();
$ip->setStartIp('192.168.1.10') // Use only setStartIp() if it's a simple IP
   ->setEnvironment(['dev', 'test']) // You can also use 'prod'
   ->setAuthorized(false);

$ipManager->saveIp($ip);

// If you need to block or authorized an IP range, you must also specify an end ip with the setEndIp() method
$ip = $ipManager->createIp();
$ip->setStartIp('10.30.0.0')
   ->setEndIp('10.30.0.255')
   ->setEnvironment(['dev', 'test'])
   ->setAuthorized(true);

$ipManager->saveIp($ip);



use Coosos\IpFilterBundle\Model\IpManagerInterface;

/** @var IpManagerInterface $ipManager */
$ipManager = $this->container->get('sl_ip_filter.ip_manager');

//All addresses (IPv4)
$range1 = $ipManager->hydrateModelWithIp('0.0.0.0/0');
$range1->setEnvironment(['dev', 'test'])
       ->setAuthorized(false);

$ipManager->saveIp($range1);

//My local network (IPv4)
$range2 = $ipManager->hydrateModelWithIp('192.168.0.0/16');
$range2->setEnvironment(['dev', 'test'])
       ->setAuthorized(true);

$ipManager->saveIp($range2);

//Another local network (IPv6)
$range3 = $ipManager->hydrateModelWithIp('fe80::/64');
$range3->setEnvironment(['dev', 'test'])
       ->setAuthorized(true);

$ipManager->saveIp($range3);