bramus / router

A lightweight and simple object oriented PHP Router

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

after router middleware not keeping states?

itskaoz opened this issue · 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.