bramus / bramus/router

after router middleware not keeping states?

Open
#205 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
1.1k
Forks
236
PR merge metrics
No merged PRs in 30d

Description

Hi!

I'm doing something like this

init stuff
$router->before stuff
$router->match stuff

$router->run(afterRouterMiddleware());

The route handling functions are setting some variables in static or singleton objects and everything runs fine between different objects. But the afterRouterMiddleware() function seems to forget all about these states. If i call the function just after $router->run() it runs fine but not as a callback.

I will write a more concrete example:

```
class AccessController
{
public static function authenticateForAdmin() {
User::getInstance()->setId(1337);
}
}
```
```
// bootstrap
$router->before('GET|POST|PUT|DELETE', '/admin/(?!authenticate)(.*)', 'AccessController@isVerifiedAsAdmin');
$router->mount('/admin', function () use ($router) {
$router->match('GET|POST', '/authenticate', 'AccessController@authenticateForAdmin');
}

$router->run(function() {
User::getInstance()->getId(); // <<=== null
});

User::getInstance()->getId(); // <<=== 1337
```

I think this behaviour is not supposed to happen.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the router->run() callback and compare its execution context with calling the same function after run(), using the bootstrap example as the reproduction. Trace how the mounted route and middleware are invoked, then verify that state set by authenticateForAdmin() remains available in the callback.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.