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.
HttpMethod::GET, HttpMethod::POST), eliminating magic strings ('GET', 'POST'). This aligns well with Laravel’s strong typing and PSR-15/PSR-7 standards, reducing boilerplate and improving IDE support (autocompletion, refactoring).Illuminate\Http\Request, Symfony\Component\HttpFoundation\Request) without coupling to framework-specific abstractions.PATCH, PROPFIND) if needed, though Laravel already handles these natively.'GET', 'POST', etc., with HttpMethod::GET, etc.composer.json), negligible impact.SEARCH, CONNECT) that aren’t covered by this package?Request::METHOD_GET (or Symfony\Component\HttpFoundation\Request::METHOD_GET) suffice, or is this package’s consistency across projects preferable?Illuminate\Http\Request (via Request::method() → compare with HttpMethod::GET).Symfony\Component\HttpFoundation\Request (if used in custom middleware).Route::get(), Route::post() → replace 'get', 'post' with constants).Phase 1: Dependency Addition
composer.json:
"require": {
"ergebnis/http-method": "^2.0"
}
composer update.Phase 2: Refactoring
'GET' → HttpMethod::GET (case-sensitive).pcre2 regex to find all occurrences:
grep -r "'(GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS|TRACE|CONNECT)'" --include="*.php" .
routes/web.php, routes/api.php).public function index(Request $request) → validate $request->method() === HttpMethod::GET).if ($request->isMethod('POST')) → if ($request->method() === HttpMethod::POST)).Phase 3: Testing
405 Method Not Allowed).Phase 4: Documentation
HttpMethod::* constants for HTTP methods").php artisan route:cache).'POTS' instead of 'POST').HttpMethod::HEAD needs to be HttpMethod::HEAD_REQUEST).MethodNotAllowedHttpException for HttpMethod::POST).POST?" → "Ah, it expects HttpMethod::POST").php artisan route:list post-migration to verify routes.composer why-not ergebnis/http-method to audit usage.SEARCH), they won’t be covered.strtoupper($method)).use Ergebnis\HttpMethod;
// Before
Route::get('/users', ...);
// After
Route::method(HttpMethod::GET, '/users', ...);
// phpstan.neon
rules:
- methodCallArgument: true
method: 'Route::\w+'
argument: 1
message: 'Use HttpMethod::* constants instead of magic strings.'
grep -E "'(GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS|TRACE|CONNECT)'" --include="*.php" . | grep -v "HttpMethod::"
How can I help you explore Laravel packages today?