Skip to main content
libphonenumber-for-php can be integrated into various PHP frameworks through third-party packages. These packages provide framework-specific features like validation rules, form helpers, and service providers.

Available Integrations

Symfony

PhoneNumberBundleOfficial Symfony bundle for phone number handling

Laravel

Laravel PhoneComprehensive Laravel package with validation rules

TYPO3

TYPO3 Phone ExtensionPhone number handling for TYPO3 CMS
These packages are maintained by third parties. While they provide excellent integration capabilities, their quality and maintenance status may vary. Always check the package’s documentation and recent activity before integrating.

Symfony Integration

The PhoneNumberBundle provides Symfony-specific features:
  • Doctrine types for phone number entities
  • Form types for phone number inputs
  • Validation constraints
  • Twig filters for formatting

Installation

composer require odolbeau/phone-number-bundle

Basic Usage

use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber as AssertPhoneNumber;

class Contact
{
    #[AssertPhoneNumber]
    private ?string $phoneNumber = null;
}

Laravel Integration

The Laravel Phone package offers:
  • Custom validation rules
  • Phone number casting
  • Database storage helpers
  • Country detection

Installation

composer require propaganistas/laravel-phone

Basic Usage

use Illuminate\Http\Request;

public function store(Request $request)
{
    $validated = $request->validate([
        'phone' => 'required|phone:US,CA',
    ]);
}

TYPO3 Integration

The TYPO3 Phone Extension provides:
  • TCA field types
  • ViewHelpers for formatting
  • Phone number validation

Custom Framework Integration

If your framework isn’t listed above, you can create a custom integration:
use libphonenumber\PhoneNumberUtil;
use libphonenumber\PhoneNumberFormat;

class PhoneNumberService
{
    private PhoneNumberUtil $phoneUtil;
    
    public function __construct()
    {
        $this->phoneUtil = PhoneNumberUtil::getInstance();
    }
    
    public function format(string $number, string $region): string
    {
        try {
            $phoneNumber = $this->phoneUtil->parse($number, $region);
            return $this->phoneUtil->format(
                $phoneNumber,
                PhoneNumberFormat::INTERNATIONAL
            );
        } catch (\Exception $e) {
            return $number;
        }
    }
    
    public function validate(string $number, string $region): bool
    {
        try {
            $phoneNumber = $this->phoneUtil->parse($number, $region);
            return $this->phoneUtil->isValidNumber($phoneNumber);
        } catch (\Exception $e) {
            return false;
        }
    }
}
When creating custom integrations, always use the singleton pattern with getInstance() to ensure optimal performance. See the Performance page for more details.

Dependency Injection

For frameworks that support dependency injection, consider registering PhoneNumberUtil as a service:
// Example service container registration
$container->singleton(PhoneNumberUtil::class, function () {
    return PhoneNumberUtil::getInstance();
});
This ensures a single instance is shared across your application.

Testing Considerations

When testing code that uses libphonenumber-for-php:
use PHPUnit\Framework\TestCase;
use libphonenumber\PhoneNumberUtil;

class PhoneNumberServiceTest extends TestCase
{
    private PhoneNumberUtil $phoneUtil;
    
    protected function setUp(): void
    {
        $this->phoneUtil = PhoneNumberUtil::getInstance();
    }
    
    public function testValidUsNumber(): void
    {
        $number = $this->phoneUtil->parse("+1 650 253 0000", "US");
        $this->assertTrue($this->phoneUtil->isValidNumber($number));
    }
}

Contributing to Integrations

If you’ve created or improved a framework integration:
  1. Document your implementation thoroughly
  2. Include comprehensive tests
  3. Follow the framework’s coding standards
  4. Consider sharing it with the community
Found a framework integration not listed here? Submit a pull request to add it to the main repository’s README.

Build docs developers (and LLMs) love