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

Http Method Laravel Package

ergebnis/http-method

Tiny PHP package providing named constants for HTTP request methods (GET, POST, PUT, DELETE, etc.). Use it to avoid magic strings and share a single source of truth across frameworks, libraries, and your own code.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require ergebnis/http-method
    

    No additional configuration is required—just autoload the package.

  2. First Use Case: Replace hardcoded HTTP method strings (e.g., "GET", "POST") with constants for type safety and IDE autocompletion.

    use Ergebnis\HttpMethod\Method;
    
    // Instead of:
    $method = "GET";
    
    // Use:
    $method = Method::GET;
    
  3. Where to Look First:

    • Constants: Explore Method class (e.g., Method::GET, Method::POST, Method::PATCH).
    • Validation: Use Method::isValid() to check if a string is a valid HTTP method.
    • Case-Insensitive Matching: Use Method::fromString() to convert strings to constants safely.

Implementation Patterns

Usage Patterns

  1. Request Handling:

    use Ergebnis\HttpMethod\Method;
    use Symfony\Component\HttpFoundation\Request;
    
    $request = new Request();
    $method = Method::fromString($request->getMethod()); // Safe conversion
    
  2. Route Definitions:

    Route::get('/users', [UserController::class, 'index']); // Use constants for clarity
    Route::post('/users', [UserController::class, 'store']);
    
  3. API Contracts:

    public function validateMethod(string $method): void
    {
        if (!Method::isValid($method)) {
            throw new \InvalidArgumentException("Invalid HTTP method");
        }
    }
    
  4. Middleware:

    public function handle(Request $request, Closure $next)
    {
        if (Method::fromString($request->getMethod()) === Method::PATCH) {
            // Custom logic for PATCH requests
        }
        return $next($request);
    }
    

Workflows

  • Type Safety: Use constants in method signatures or return types (e.g., public function create(Method $method)).
  • IDE Support: Autocomplete and static analysis tools (e.g., PHPStan) recognize constants as valid HTTP methods.
  • Testing: Mock HTTP methods in tests using constants:
    $this->assertEquals(Method::POST, Method::fromString("post"));
    

Integration Tips

  • Laravel Routes: Combine with Laravel’s route model binding or API resources for consistency.
  • Symfony Components: Works seamlessly with Symfony\Component\HttpFoundation\Request/Response.
  • Custom Validation: Extend Laravel’s FormRequest validation:
    public function rules()
    {
        return [
            'method' => ['required', Rule::in(array_column(Method::cases(), 'value'))],
        ];
    }
    

Gotchas and Tips

Pitfalls

  1. Case Sensitivity:

    • Method::fromString() converts strings to uppercase (e.g., "post"Method::POST).
    • Avoid mixing strtoupper() with fromString() to prevent double-conversion.
  2. Non-Standard Methods:

    • The package includes standard methods (GET, POST, etc.) but not custom methods (e.g., X-MY-CUSTOM-METHOD).
    • Use Method::isValid() to filter out non-standard methods.
  3. Deprecation:

    • New HTTP methods (e.g., PURGE) may not be included immediately. Check the releases for updates.

Debugging

  • Invalid Method Errors:

    • Use Method::isValid($method) to validate before processing.
    • Log or throw exceptions for unsupported methods early in the request lifecycle.
  • IDE Issues:

    • If autocompletion fails, ensure your IDE’s PHPStan/Psalm plugin is configured to recognize the package’s constants.

Config Quirks

  • No Configuration: The package is stateless—no config/http-method.php exists. All functionality is self-contained.

Extension Points

  1. Custom Methods:

    • Extend the Method enum (PHP 8.1+) or create a wrapper class for project-specific methods:
      class CustomMethod extends Method
      {
          public const PURGE = 'PURGE';
      }
      
  2. Integration with Laravel:

    • Create a helper trait for common use cases:
      trait UsesHttpMethods
      {
          protected function isGetRequest(): bool
          {
              return request()->method() === Method::GET;
          }
      }
      
  3. Localization:

    • Translate method names for user-facing messages (e.g., "Method {method} is not allowed"). Use Method::value to get the raw string:
      __('Method :method is not allowed', ['method' => Method::POST->value]);
      
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.
andydefer/laravel-cluster
testo/fiber
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
spatie/laravel-javascript-views