bramus / bramus/router

Undefined array key "GET" in /vendor/bramus/router/src/Bramus/Router/Router.php on line 292

Open
#218 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
PHP
Stars
1.1k
Forks
236
PR merge metrics
No merged PRs in 30d

Description

Hi,

This seems not a big issue, but I thought I'd report it anyway.

When you make a GET request to a page that does not exist, which should prompt a 404, and there are no defined GET routes, then you get this error message:
"Undefined array key "GET" in /vendor/bramus/router/src/Bramus/Router/Router.php on line 292"

If you define a GET route just as a placeholder. The error disappears.

Same situation is valid for POST requests.

Example:

```
$router = new \Bramus\Router\Router();

$router->set404(function () {
header('HTTP/1.1 404 Not Found');
echo'404: Page not found!';
});

$router->post('/upload', function () {
// some code
});

$router->run();
```

Make a GET request (just browse) to any url other than "/upload" to trigger a 404 and I get the error message I mentioned above.

Workaround I am using:

```
$router->match('GET|POST', '/get-post-route-placeholder', function () {
// This Route is here so that there is at least one GET and one POST route defined.
// When there is no GET or no POST routes, a GET or POST request to a non-existing URL that triggers set404() prompts an error:
// Undefined array key "GET"(or "POST") in /vendor/bramus/router/src/Bramus/Router/Router.php on line 292
});
```

Did not test any other HTTP methods.

bramus/router is version 1.6.1 installed via composer.

PHP 8.1.2 on Ubuntu 22.04.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at vendor/bramus/router/src/Bramus/Router/Router.php line 292 and trace how run(), route matching, and set404() handle a request when no GET or POST routes exist. Reproduce the case on PHP 8.1 with the example, then verify that nonexistent GET and POST requests invoke the 404 handler without an undefined-array-key warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.