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

Net Uri Laravel Package

phrity/net-uri

Lightweight PSR-7 UriInterface and PSR-17 UriFactory implementation not tied to HTTP messaging. Supports any valid scheme plus helpful extras like query item helpers, component access, equals/string/json support, and immutable with* methods.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require phrity/net-uri
    

    Add to composer.json under require or require-dev based on use case.

  2. Basic Usage:

    use Phrity\Net\Uri;
    $uri = new Uri('https://example.com/path?query=value');
    echo $uri->getPath(); // Outputs: /path
    
  3. First Use Case: Replace Laravel’s default URI handling in a custom HTTP client or middleware:

    use Phrity\Net\Uri;
    $requestUri = new Uri($request->getUri());
    $modifiedUri = $requestUri->withQueryItem('debug', 'true');
    

Where to Look First

  • Documentation: Uri Class Docs and UriFactory Docs for method signatures and modifiers.
  • Examples: The README.md section demonstrates core functionality like query manipulation and path normalization.
  • Modifiers: Focus on REQUIRE_PORT, NORMALIZE_PATH, and IDN_ENCODE for common edge cases.

Implementation Patterns

Usage Patterns

  1. Immutable URI Modifications: Use with* methods for safe, functional-style updates:

    $uri = (new Uri('http://example.com'))
        ->withScheme('https')
        ->withQueryItem('token', 'abc123');
    
  2. Query Parameter Handling: Leverage helper methods for dynamic query updates:

    $uri = new Uri('http://example.com?page=1');
    $uri->withQueryItems(['page' => 2, 'sort' => 'desc']);
    
  3. Path Normalization: Clean up paths with modifiers:

    $uri = new Uri('http://example.com/a/./b/../c');
    $normalized = $uri->withPath($uri->getPath(), Uri::NORMALIZE_PATH | Uri::ABSOLUTE_PATH);
    
  4. Factory Integration: Centralize URI creation via UriFactory:

    $factory = new Phrity\Net\UriFactory();
    $uri = $factory->createUri('https://example.com');
    
  5. PSR-17 Compliance: Use the factory in Laravel’s HTTP clients (e.g., Guzzle) for consistency:

    $client = new GuzzleHttp\Client([
        'base_uri' => $factory->createUri('https://api.example.com'),
    ]);
    

Workflows

  1. API Request Construction: Dynamically build URIs with query parameters:

    $baseUri = new Uri('https://api.example.com/v1');
    $resourceUri = $baseUri
        ->withPath('/users')
        ->withQueryItem('limit', $request->input('limit'));
    
  2. Middleware URI Manipulation: Modify request URIs in middleware:

    public function handle($request, Closure $next) {
        $uri = new Uri($request->getUri());
        $uri = $uri->withQueryItem('locale', app()->getLocale());
        $request = $request->withUri($uri);
        return $next($request);
    }
    
  3. Route Generation: Generate canonical URIs for redirects or links:

    $uri = new Uri('/products/{id}');
    $canonical = $uri->withPath(str_replace('{id}', $product->id, $uri->getPath()));
    
  4. Non-HTTP URIs: Handle custom schemes (e.g., app://, mailto:):

    $uri = new Uri('mailto:user@example.com?subject=Hello');
    $uri->getScheme(); // Outputs: mailto
    

Integration Tips

  1. Laravel Service Provider: Bind the factory to the container for dependency injection:

    $this->app->bind(
        Psr\Http\Message\UriInterface::class,
        fn() => (new Phrity\Net\UriFactory())->createUri('')
    );
    
  2. Replace Default URI Handling: Override Laravel’s UrlGenerator or Request URI parsing where needed:

    // In a custom Request class
    public function getUri() {
        return new Phrity\Net\Uri(parent::getUri());
    }
    
  3. Testing: Use the package’s equals() method for URI assertions:

    $this->assertTrue(
        (new Uri('https://example.com'))->equals('https://example.com')
    );
    
  4. IDN Support: Encode/decode internationalized domains:

    $uri = new Uri('https://ηßöø必Дあ.com');
    $encoded = $uri->getHost(Uri::IDN_ENCODE);
    
  5. Performance: Cache UriFactory instances for repeated use:

    $factory = new Phrity\Net\UriFactory(); // Reuse this instance
    

Gotchas and Tips

Pitfalls

  1. Port Omission: Default ports (e.g., 80 for HTTP) are omitted unless REQUIRE_PORT is used:

    $uri = new Uri('http://example.com:80');
    $uri->getPort(); // Returns null (default port)
    $uri->toString(Uri::REQUIRE_PORT); // Includes `:80`
    
  2. Path Normalization: NORMALIZE_PATH may behave unexpectedly with relative paths:

    $uri = new Uri('http://example.com/./path/../');
    $uri->getPath(Uri::NORMALIZE_PATH); // Returns '/' (root)
    
  3. Query Parsing: Query strings with special characters (e.g., &, =) may require URI_DECODE:

    $uri = new Uri('http://example.com?key=value&key2=value2');
    $uri->getQueryItem('key', Uri::URI_DECODE); // Decodes URL-encoded values
    
  4. IDN Encoding: IDN_ENCODE modifies the host permanently unless combined with withHost():

    $uri = new Uri('https://ηßöø必Дあ.com');
    $uri->getHost(Uri::IDN_ENCODE); // Encodes but doesn’t modify $uri
    $uri->withHost($uri->getHost(), Uri::IDN_ENCODE); // Updates $uri
    
  5. PHP 8.1+ Requirement: The package requires PHP 8.1+ (as of v2.2). Ensure your Laravel app meets this requirement.

Debugging

  1. Invalid URIs: The package may silently accept malformed URIs. Validate with:

    if (!$uri->getScheme()) {
        throw new InvalidArgumentException('Invalid URI scheme');
    }
    
  2. Modifier Conflicts: Combine modifiers carefully (e.g., ABSOLUTE_PATH + NORMALIZE_PATH):

    $uri->withPath('relative/path', Uri::ABSOLUTE_PATH | Uri::NORMALIZE_PATH);
    
  3. Query Item Overrides: Use withQueryItems() to replace all query parameters:

    $uri->withQueryItems(['new' => 'value']); // Overrides all existing items
    

Tips

  1. Laravel URL Helper: Extend Laravel’s URL::to() with custom logic using this package:

    URL::macro('custom', function ($path, $query = []) {
        $uri = new Uri($path);
        return $uri->withQueryItems($query)->__toString();
    });
    
  2. Immutable Chaining: Chain with* methods for fluent URI construction:

    $uri = (new Uri('http://example.com'))
        ->withPath('/api/v1')
        ->withQueryItem('format', 'json');
    
  3. Component Manipulation: Use getComponents() and withComponents() for bulk updates:

    $components = $uri->getComponents();
    $components['scheme'] = 'https';
    $uri = $uri->withComponents($components);
    
  4. Testing Edge Cases: Test with:

    • Internationalized domains (e.g., https://例子.测试).
    • URIs with ports (e.g., http://example.com:8080).
    • Relative paths (e.g., /path/../to).
  5. Performance Optimization: Avoid repeated URI parsing by reusing Uri instances:

    $baseUri = new Uri('https://api.example.com');
    // Reuse $baseUri across requests
    
  6. Laravel Route Parameters: Parse route URIs with this package

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.
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor