Hi, Im trying to use react inside the openemr to build better forms, but I don’t wanna use apis from openemr API, I wanna make PHP simply API endpoints, because the use is just internally, I don’t need use the builtin API system.
But, the problem is every time I need use the globals, I got “Site ID is missing from session data!”, any clever way to do this, just a simply example (not real)
include_once("…/…/interface/globals.php");
// Echo globals encoded by json
echo json_encode($GLOBALS[‘webroot’]);
if I use this or any globals, “Site ID is missing from session data!” is the result
I don’t think I can really help, but this sounds interesting. Let me see if I understand what you’re trying to do.
You want to create a react website that calls a PHP backend. You don’t want to call any openEMR api’s but you want to make your own?
But with that you still want to access globals?
If I understand, and I’m not sure I do… It sounds like you need to create another PHP website that contains the APIs that you create/need. Sounds like you want the globals data, I would go search openEMR code and see where in the db those values are stored. then one of the API’s I create would return the global value that I need.
I maybe totally misunderstanding your plan though.
If this is what you’re trying to accomplish, maybe this new API website and new React website should use OAuth2, set it up to work with openEMR. Another option would be add your controller methods in openEMR code and use OAuth2 to make your UI connect to it.
I’m just thinking out loud. and what I think you want to do sounds interesting. I would like to do something similar with an Angular app…
Hey Brad, yeah, you kinda undestand the idea, but let me you explain better.
We have some forms to mental health, and some forms are complex, a lot fields validations, and my idea is using react components inside. So inside of interface/forms, I’m creating a react files, and when the form is called I’m using react, but for use any function on PHP, like sqlstatemnt I need have access to globals. And if I try fetch any PHP file inside the form, the error happens.
Imagine the flow
Click on the form link and the link open a new tab inside openemr
I don’t know how much you have invested in your current forms implementation but does the new Questionnaire form system and National Library of Medicine form builder (https://lhcformbuilder.nlm.nih.gov/) handle your field validation requirements?
What kinds of complexity are you dealing with? Question branching? Field summation? Threshold criteria? I’ve got a lot of experience in the assessment space and I’m interested in what your needs are in the mental health arena.
It seems like you’d want to use something that comes native in OpenEMR and that will be supported by the broader community if it meets your requirements.
As for the site not found error you are dealing with in your API. Can’t you include the site parameter in your endpoint url?
IE if your react app is at
<openemr_root_dir>/interface/test-form/index.html
And your api is at <openemr_root_dir>/interface/test-form/api.php
Then your react app should be calling your endpoint using the following (assuming you are not running on multi-site).
https://<domain_name>/interface/test-form/index.html?site=default
The other mechanism is to hard-code the site in the session $_SESSION[‘site_id’] = ‘default’ before you run globals.php. You’ll need to include the SessionUtils class manually and run SessionUtils::coreSessionStart();
The second option is more cumbersome in my mind than just including the parameter in your get request.
The other approach would be to use mod_rewrite and .htaccess to rewrite your URLs to include the site parameter in the API endpoint to make this follow more of the REST pattern. That’s how we handled it in the standard and REST apis.
As an aside, if you ever plan on supporting multi-site installations (multiple clinics or locations running off a single codebase), you need to make sure you architect your app to inject the site id inside your react app.
Is your react app not sending the OpenEMR site cookie? Look at your developer console and make sure the session cookies are being sent.
I’m assuming you are running the react app inside the same hosted origin as OpenEMR and that you have authenticated. If you haven’t authenticated and don’t have a valid OpenEMR session cookie inside OpenEMR then you will be blocked in the globals. If you are running your app on a different hosted location (IE different port, hostname, or protocol) then you’ll get blocked from sending cookies due to the OpenEMR SameSite cookie attribute.
You can change it on your own hosted solution thought it’s not recommended. The settings are in that SessionUtil class. For security reasons we won’t change the cookie in the core OpenEMR installation.
People that want to handle cross domain communication are recommended to use the standard or FHIR apis for those purposes.
Subdomains that are one domain under your hosted domain should still work as long as you control both the hosted domain and the subdomain with the SameSite attribute.