We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Dynamic Routes

Hi!

How can i check dynamic route for JWT? For example. We have routes which need to ignored for JWT

    $di->set('routeIgnore', function() {
        return array(
            '/v1/auth',
            '/v1/logins'
        );
    });

I mean, https://localhost/v1/logins - OK, how can i add dynamic routes like this https://localhost/v1/logins/[0-9]

This is my check code for JWT

    if(!in_array($app->router->getRewriteUri(), $app->routeIgnore) && $app->jwtEnabled) {
        if(!$app->request->getHeader('Authorization')) {
        error
        ...
        }
    ...
    }

Is any way to add dynamic route like this

    $di->set('routeIgnore', function() {
            return array(
                '/v1/auth',
                '/v1/logins/[0-9]'  <---- it's doesn't work
            );
        });


2.8k

Good day :)

My decision

  // for /v1/logins/$id:[0-9]
  function checkIgnored($app) {
      if(preg_match('/[(\W|^)logins(\W|$)]\d+/', $app->router->getRewriteUri(), $matches)) {
          return str_replace($matches[0], '', $app->router->getRewriteUri());
      } else {
          return $app->router->getRewriteUri();
      }
  }

  if(!in_array(checkIgnored($app), $app->routeIgnore) && $app->jwtEnabled) { ... }

I will be grateful if you know more elegant decision :)