I need all your wisdom today.
So I work on a big legacy project at my company and I have to come up with a solution to share constantes between our back-end in PHP 5 (MVC architecture, no framework) and our front-end with JS (angularJS).
In our backend, we have classes that manage our entities, in which we define constantes we work with. These are constantes like char that we use to refer to different possible values of a field.
class Task {
const STATUS_TODO = 'T';
const STATUS_DONE = 'D';
}
So when we need to make a condition on these constants, we do like so :
if ( $variable === Entity::STATUS_TODO ) { //do something }
But we don't have such management in our front-end, so you often find conditions like :
if ( variable === 'T' ) { //do something }
Which is a terrible thing to do, we all know it.
The point would be to share constantes between the back and the front, so that if we need to change something or add a new constantes, we just have to do it in one place.
I know this is a pretty common problem in web development, and I was wondering how you guys would advise to solve it.
Thanks for your time, have a great day
If possible, collect your constants within special (abstract) classes within a central directory like app/Constants in PHP and src/constants in JS. It is then possible (though I never did that on my own) to create an update-script that will parse the constants from one project and sync them to the other. Both languages can be generated pretty easy so you can even approach a bi-directional solution.
Finally you could set up CI/GitHooks to execute the sync on every commit/push/merge.
In the past I ended up with copying the constants from the PHP project to the JS-Client and then re-format them for JS via RegEx.