- How do I install darsyn/ip in a Laravel project?
- Run `composer require darsyn/ip` in your project directory. The package is autoloaded via Composer’s PSR-4 autoloader, so no additional configuration is needed for basic usage. Laravel-specific helpers like `Ip::fromRequest()` will work immediately in middleware or controllers.
- Does this package support IPv6 addresses?
- Yes, the package fully supports IPv6 parsing, validation, and comparison. It uses PHP’s built-in `filter_var` for IPv6 validation and provides consistent APIs for both IPv4 and IPv6. Tested with dual-stack environments, but ensure your Laravel stack (e.g., `Request::ip()`) handles IPv6 correctly.
- Can I use this for rate limiting or security middleware?
- Absolutely. The package includes CIDR range checks (e.g., `Ip::isInRange($ip, '192.168.0.0/16')`) and immutable value objects, making it ideal for security middleware. For example, you can validate IPs early in the request lifecycle or enforce subnet-based access rules.
- What Laravel versions does darsyn/ip support?
- The package is officially tested with Laravel 9 and 10. While it avoids breaking changes, always verify compatibility with your Laravel version by checking the package’s changelog or running `composer require darsyn/ip` in a test environment. PHP 8.1+ is required.
- How do I store IP addresses in the database?
- Store IPs as strings (e.g., `ip_address` column) and parse them with `Ip::fromString($ip)` when needed. For advanced use cases, serialize `IpAddress` objects to JSON or use a dedicated column type if your database supports it (e.g., PostgreSQL’s `inet` type).
- Are there performance concerns for high-traffic apps?
- The package is optimized for performance, with efficient CIDR range checks and minimal overhead. For high-throughput systems (e.g., 10K+ requests/sec), benchmark the package’s `isInRange()` method, but it should handle most use cases without issues. Avoid overusing complex CIDR logic in hot paths.
- How do I mock IpAddress objects in unit tests?
- Use Laravel’s built-in mocking tools or PHPUnit’s `createMock()` to simulate `IpAddress` objects. For example, mock `Ip::fromString()` to return predefined objects in tests. The package’s immutable design makes it easy to test equality, ranges, and validation logic without side effects.
- Can I use this package outside Laravel (e.g., CLI tools)?
- Yes, the core functionality works in plain PHP, but Laravel-specific helpers (e.g., `Ip::fromRequest()`) won’t be available. Focus on the `Ip` facade or static methods like `Ip::fromString()` for standalone scripts or APIs. The package remains lightweight and dependency-free.
- What if I need geolocation or advanced IP services?
- This package handles parsing, validation, and CIDR logic but doesn’t include geolocation. For geo-based features, integrate with external services (e.g., MaxMind, IP-API) after validating IPs with `darsyn/ip`. The package’s value objects make it easy to pass IPs to third-party libraries.
- How do I migrate from string-based IP logic to this package?
- Start by replacing raw strings with `Ip::fromString($ip)` in non-critical areas (e.g., logging). Use feature flags to toggle between old and new logic during migration. For databases, update queries to parse IPs on retrieval. Prioritize security-sensitive flows (e.g., authentication) last to minimize risk.