Best way to determine dev mode vs prod mode?

When working on a project you most commonly have a dev environment and a prod environment. Things like database connection information and paths might be different between the 2, so my question is, what is the standard practice to determine between the 2? For example, would you use something like the current ip address to know if your web app is in either prod or dev mode? So if it is currently 127.0.0.1, then you know its local and load these settings accordingly. Or would it be better to store this option via the database? So you query the database and based on the value, it will tell you if you are currently in dev or prod? Please let me know what a good standard practice is for this. Thanks!

This is what I do:

<?php
if (filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_URL) == "localhost") {
  define("DATABASE_HOST", 'localhost'); // Usually localhost for the local server:
  define("DATABASE_NAME",  'simplecms'); // Name of Database:
  define("DATABASE_USERNAME", 'username'); // Usually root for local server:
  define("DATABASE_PASSWORD", 'password');
} else {
  // Remote connections constants go here:
}

I have it in a separate file and folder (not root), don’t know if that is how others do it, but it works for me. Though I do most of developing locally before going live. I don’t have to worry about the connections and it’s easy to tell what your working on by just looking at the url in the browser.

Using Environment Variables is a good option here. You define them system-wide, and the PHP app reads from that via getenv. Another approach is having two config files, for example: config/local/config.php and config/global/config.php. Set your PHP application so that it reads both of those, but overwrites the settings from global config, so that you can have one set of user/pass credentials for global, and one for your local development environment. You put the local file into .gitignore, and this will make sure it doesn’t end up in everyone else’s repository - this also makes sure every dev on the team can have their own local credentials, without conflicting with other people’s. This way, your application will automatically be in “local” mode when you’re developing (because the local config file exists) and will automatically be in production/global mode when deployed elsewhere. Any of these approaches work.

I would advise against setting the application state in the database. Your database shouldn’t have anything to do with whether or not your app is in development/debug mode.

I would also advise against using the URL to discern different environments, as this makes it more difficult to use virtual machines and virtual hosts.

4 Likes

Thank you!

No problem. Remember to upvote the answer if you’re happy with it :slight_smile:

Ok, upvoted… I think. New to Sitepoint, so please excuse the newb in me. Hey, talking about dev/prod environments I originally was planning to use GIT, however do you think GITHub would be just easier? Do you use it? I see I have to pay a small fee to keep my projects private, but other than that its already hosted and backed up. Seems better than setting up GIT locally and doing that. Do you agree?

No biggie, we’re all “new” here, considering this forum just launched on a new software stack :smile:

If you want free private projects, you can use Bitbucket. BB and Github are essentially the same, BB just lacks the community, but you’ll get private repositories. I use Github in almost all cases, as I rarely work on closed source code. For such instances, the client usually pays the private repository fee. In either case you’ll be using Git, as Github and Bitbucket are merely means of easily and quickly backing up your Git repo online.

1 Like

I guess my confusion is this, do I still have to install GIT locally and setup repos? And BB/GitHub just allow me to upload my repos online? Or if I use BB/Github, then I do not need to install GIT locally since BB/Github do all that within their app?

Like I said above, in either case you’ll still be using Git locally. BB/Github are just hosting and collaboration solutions that build on that. They are in no way permanently attached, and you can even have the repo “synced” (aka pushed) to multiple online endpoints (aka remotes), even both on BB and Github at the same time. They’re just hosting for your local repo.

This screencast might help.

1 Like

I will take a look, thanks. I understand what you were saying, I was just wondering if I need to install GIT locally or if I just intall GitHub locally, it will install GIT for me. Thanks for the link to the screencast, will check it out.

What you install depends on your environment setup.
I do all my development in virtual machines powered by Vagrant so I can spin up multiple projects side by side on different PHP versions without disturbing my host machine, and as such Github’s tools aren’t very useful for me - I have nothing to use them on on my host machine.

My approach is to have Git installed in the VM I’m developing a project in, so it’s fully isolated, and then I just push stuff online through the command line / terminal. No need for Github tools at all - you use Github by adding a github remote and just saying git push, and Github tools are just a handy wrapper around those commands. See the screencast, it should explain things better than I can here - your best bet is just giving it all a try. It’ll be the fastest way to learn. Make a couple sample projects/repos and push them online, things will clear up instantly and all the errors are highly googleable.

2 Likes

Another great post, thanks again! Wish I had a mentor like yourself. Trying real hard not to fall into that “lone wolf” category where I can only program alone, know what I mean?

Of course, it’s a common pitfall. There’s no better way around it than staying curious. You might want to try PhpMentoring.

Practically, think up a project, work on it, ask around when you hit a wall, and don’t be afraid to do complete rewrites. The beginning consists almost entirely of these rinse and repeat cycles. Most importantly, make sure the project is real - something you want to use, not something that’s straight out of a tutorial. Build yourself a life helper app, something to manage you finances, something into which you can put taggable and searchable diary posts, something which can be used to record your mood and output graphs on how days and times of day influence you - there’s nothing too complex, just go for it.

But we’ve drifted far too off topic now, let’s pick this up in another if you’re still curious about other aspects :smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.