Weave 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

A small Composer package providing named constants for HTTP request methods, so you can reference GET, POST, PUT, PATCH, DELETE, and others by name instead of magic strings in your PHP code.

View on GitHub
Deep Wiki
Context7

Getting Started

ergebnis/http-method is a constants-only package: a handful of interfaces that give names to HTTP request method strings. There is no service provider, no config file, no runtime behavior. Integration is one composer require and an import.

composer require ergebnis/http-method

Check the PHP constraint in composer.json first — the maintainers drop EOL PHP versions aggressively (2.8.0 supports PHP 8.5, but 2.3+ already dropped 7.4). If you are on an older Laravel/PHP combination, pin to a compatible tag.

Where to look first:

  • src/Method.php — the core interface (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, CONNECT, TRACE), RFC 7231 + RFC 5789.
  • src/Method/WebDav.php — the WebDAV/CalDAV aggregate (PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, MKCALENDAR, SEARCH, etc.).
  • src/Method/Vendor/SquidCache.php and src/Method/Vendor/VarnishCache.php — the de-facto cache verbs PURGE and BAN.

First realistic use case in a Laravel app: calling an endpoint with a non-standard verb, or making a request through the HTTP client without scattering 'POST'/'GET' string literals through your code.

use Ergebnis\Http\Method;
use Illuminate\Support\Facades\Http;

$response = Http::send(Method::HEAD, 'https://example.com/health');

The README example uses a PSR-18 request factory, which is the canonical form:

use Ergebnis\Http\Method;
use Psr\Http\Client;
use Psr\Http\Message;

/** @var Message\RequestFactoryInterface $requestFactory */
$request = $requestFactory->create(
    Method::GET,
    'https://localheinz.com/articles/'
);

/** @var Client\ClientInterface $httpClient */
$httpClient->sendRequest($request);

Implementation Patterns

Pick the narrowest interface. Use Ergebnis\Http\Method for everyday REST calls; only pull in Method\WebDav or Method\Vendor\* when you actually speak those protocols. Importing the WebDAV interface into a controller that only does GET/POST makes the intent less clear, not more.

Laravel HTTP client. Http::send() takes an explicit method and accepts the constants directly. For fluent calls, the verb-specific helpers (Http::get(), Http::post()) don't need constants, so reserve the package for the odd verbs:

use Ergebnis\Http\Method;

Http::send(Method::HEAD, $url);                       // health checks
Http::send(Method::OPTIONS, $url);                    // capability discovery / CORS preflight
Http::send(Method\Vendor\VarnishCache::PURGE, $url);  // cache invalidation

Guzzle / PSR-7. The constants are plain strings, so they slot into anything accepting a method string:

use Ergebnis\Http\Method;
use GuzzleHttp\Client;

(new Client())->request(Method::DELETE, $uri, ['json' => $payload]);

Routing. Laravel's Route::match() uppercases what you pass, so the constants work there too, which keeps your route table free of literals:

Route::match([Method::GET, Method::HEAD], '/status', StatusController::class);

For verb-agnostic middleware or request logging, compare against constants rather than in_array($request->method(), ['GET', 'POST']):

if ($request->method() === Method::GET) {
    // ...
}

Allowlists and validation. Because these are interface constants with no accessor, you need reflection to turn them into an array for Rule::in():

use Ergebnis\Http\Method;
use Illuminate\Validation\Rule;

$methods = array_values((new ReflectionClass(Method::class))->getConstants());

$request->validate(['method' => ['required', Rule::in($methods)]]);

If reflection-per-request bothers you, hoist it into a cached config or a small final class HttpMethods wrapper.

Cache purging. This is where the vendor interfaces earn their keep — most HTTP abstractions know nothing about PURGE/BAN:

use Ergebnis\Http\Method\Vendor\SquidCache;
use Illuminate\Support\Facades\Http;

Http::withHeaders(['Host' => 'example.com'])
    ->send(SquidCache::PURGE, 'http://cache.internal/');

WebDAV clients. When you build a WebDAV or CalDAV integration against a Nextcloud/SabreDAV server, the aggregate interface saves you from looking up MKREDIRECTREF and ORDERPATCH spellings:

use Ergebnis\Http\Method\WebDav;

$client->request(WebDav::PROPFIND, $collection, [
    'headers' => ['Depth' => '1'],
    'body'    => $propfindXml,
]);

PHP 8.1+ enums. If you want exhaustiveness or match support, map these into a backed enum in your own code and keep the package as the string source of truth:

enum HttpVerb: string
{
    case Get = Method::GET;
    case Post = Method::POST;
    // ...
}

Testing. Assert with assertSame and the constant — failures read better, and a typo becomes a fatal error instead of a silent mismatch:

$this->assertSame(Method::PATCH, $request->method());
Http::assertSent(fn ($req) => $req->method() === Method::PURGE);

Static analysis. PHPStan/Psalm will infer the literal string type from the interface constant, so passing Method::GET where 'GET' is expected passes cleanly. It also flags you when you feed a constant into a context expecting an enum or a different method interface.

Gotchas and Tips

  • Constants are not behavior. There is no Method::isValid(), no normalization, no case handling. Comparing Method::GET to $request->method() is plain string comparison, and case matters (see below).
  • HTTP methods are case-sensitive. RFC 7231 defines them as uppercase tokens; the constants are uppercase. Never strtolower() a constant to feed a helper that "expects" a verb — several clients and servers treat get differently from GET, or reject it outright.
  • Method spoofing hides the real verb. In Laravel, $request->method() returns the spoofed method when _method or X-HTTP-Method-Override is present, but the wire request was POST. If your middleware logic depends on Method::PUT versus Method::POST, know which one you are reading — use $request->getRealMethod() when you need the actually-transmitted verb.
  • You cannot instantiate or inject these. They are interfaces with no methods. app(Method::class) will fail; do not assume container resolution works. There is no facade, no config key, no publishable assets.
  • Implementing the interface means inheriting the constants. class Foo implements Method gives Foo::GET as well as Method::GET. That is occasionally handy, more often surprising, and it can collide with a constant you already declared. Implementing both Method and Method\WebDav is legal only because the overlapping constants share the same values — if you build your own interface with a conflicting GET, PHP fatals.
  • Method does not include WebDAV verbs. Method::PROPFIND does not exist. Reach for Method\WebDav::PROPFIND. Developers habitually import the base interface and then wonder why autocompletion stops at TRACE.
  • PURGE and BAN are conventions, not standards. Squid and Varnish accept them, but CDNs and load balancers in front may reject or drop them. They also are not covered by CORS preflight, so browser-side use is a non-starter.
  • Not a status-code library. The README itself points at teapot/status-code for response codes. Don't try to derive status constants from this package.
  • Version/namespace history. The package moved from Localheinz\Http to Ergebnis\Http at 2.0.0. Copied snippets from older blog posts will reference the wrong namespace. The replace directive bug from 2.0.0 was fixed in 2.0.1, so don't pin 2.0.0.
  • PHP support churn. Minor releases routinely drop EOL PHP versions. That's fine for applications on current PHP, but libraries supporting PHP 7.x/8.0 should constrain to ^2.2-ish ranges or avoid the dependency.
  • Reflection-based allowlists leak extra constants. (new ReflectionClass(Method::class))->getConstants() on Method\WebDav returns the base verbs too, since it's an aggregate. If you need a strict WebDAV-only list, enumerate explicitly.
  • CONNECT and TRACE are rarely usable. Many HTTP clients, proxies, and frameworks block them. The constants exist for completeness, not as an invitation to route TRACE through your app.
  • Extension points are effectively none. If you need a vendor verb the package doesn't ship (e.g. REPORT for CalDAV, PURGE variants on a specific CDN), the options are a PR upstream or a one-line local constant. Don't wrap the package in an abstraction just to add a string — define your own interface and implements Method where useful.
  • Triviality is the point. The whole value is IDE autocompletion and typo-proofing. If your codebase already has a house HttpMethod enum or uses the constants on Symfony\Component\HttpFoundation\Request, adding this package is duplication. Adopt it where you have magic strings, not everywhere.
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.
robertmarney/lara-hierarchial-collections
simonschaufi/rule-doc-generator
emissaoja/emissaoja
blackbricksoftware/gravityforms-civicrm-form-processor
al-rahabi/fancy-crud
jinas/dhivatar-component
mltstephane/laravel-quality
bernskiold/laravel-talentlms
bernskioldmedia/laravel-form-architect
youbar/easy-crud
c-point-labs/laravel-hostyourai
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo