Michael Lenaghan Posted April 12 Share Posted April 12 I know that in PHP everything is built up and torn down on each request — so a global variable would be a reasonable place to store a value for the duration of a request. Still: is there a better ProcessWire-specific answer? For example, maybe there's a spot to put such things that debug tools know about? Link to comment Share on other sites More sharing options...
bernhard Posted April 13 Share Posted April 13 $session->myVariable = "MyValue"; // or wire()->session->myVariable = "MyValue"; It will be available for the session of the user and will only be available/editable by the server side. Link to comment Share on other sites More sharing options...
Michael Lenaghan Posted April 13 Author Share Posted April 13 Thanks, Bernhard. I know about "session scope". I was wondering if there was something similar for "request scope." (Something other than global variables, that is.) Link to comment Share on other sites More sharing options...
bernhard Posted April 13 Share Posted April 13 I'm not sure I understand ... For one request you can store your variable wherever you want (and where it is allowed), for example you can set $config->foo = "Foo!" somewhere and later you can access that runtime property. On the next request $config->foo will be null again unless you set it somewhere. Until PHP8.2 it was possible to use $wire for that as well, eg $wire->foo = "Foo!", but since PHP8.2 setting dynamic properties is deprecated. Link to comment Share on other sites More sharing options...
Michael Lenaghan Posted April 13 Author Share Posted April 13 Yes, understood. I was thinking that if there was an official way to do it — like the deprecated example you just gave! — then tools like Tracy Debugger would know about it, and show it. But it looks like the answer to my question is "no!" Thanks, Bernhard. Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted April 14 Share Posted April 14 Hey, @Michael Lenaghan. The setting() function might be a fit. 3 Link to comment Share on other sites More sharing options...
Michael Lenaghan Posted April 14 Author Share Posted April 14 That is very interesting, thank you! For others: The `setting()` function is defined here. It's just a one-line wrapper around the `wireSetting()` function, which is define here. (`wireSetting()` is a small wrapper around a static var named `$settings`.) Here's the way `setting()` is described: /** * Get or set a runtime site setting * * This is a simple helper function for maintaining runtime settings in a site profile. * It simply sets and gets settings that you define. It is preferable to using ProcessWire’s * `$config` or `config()` API var/function because it is not used to store anything else for * ProcessWire. It is also preferable to using a variable (or variables) because it is always * in scope and accessible anywhere in your template files, even within existing functions. * * *Note: unlike other functions in the Functions API, this function is not related to API variables.* Perfect! 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now