router icon indicating copy to clipboard operation
router copied to clipboard

after router middleware not keeping states?

Open itskaoz opened this issue 1 year ago • 0 comments

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.

itskaoz avatar Apr 30 '23 15:04 itskaoz