Passing variables to route files?

Hello,

I have organized my API routes into multiple php files. In the beginning of each, I define the API paths as variables.

e.g.
$apiPublicBase = "/api/public/v1";

I am now looking to:

  1. Define them in one place, once, for all route files.

  2. Even better if I could access the same definitions in my javascript application. Is this possible?

Thanks

I believe you could use the configuration file. See : https://github.com/userfrosting/UserFrosting/blob/develop/app/sprinkles/core/routes/routes.php#L13

$config = $app->getContainer()->get('config');

$app->get('/' . $config['assets.raw.path'] . '/{url:.+}', 'UserFrosting\Sprinkle\Core\Controller\CoreController:getAsset');

If your variable is in a config file, it can used in both routes and passed to Javascript.

1 Like