Question

I am trying to work what the point of the environments folder is.

Originally I had the idea that you could point the webserver to the different dev and prod folders in the environment folder but after reading up a bit I realise this is not the case.

In Yii 1 you would solve this by just having multiple index.php's i.e.:

  • index.php
  • index-local.php

So the question is what benefit does this new environment structure actually give me over the old way?

Was it helpful?

Solution

I've found environments very useful in allowing me to keep a common code base for multiple client projects (based on Yii App Advanced) and setting up a different environment for each specific client, keeping their custom code private and separate.

To do this I store the environments folder in a separate git repo from the rest of the code and pull down the relevant folder on a client/project basis.

This lets me use a base common code for all projects and add/override any file for a specific client or project whilst still allowing separate dev/prod config settings. If the client uses other developers too, they are also catered for. In this way, only common code I choose will be shared amongst clients and custom code will be kept private.

I've also moved the composer.json file into the environments folder so I can pull in different extensions per client/project keeping those private too.

That init command can be a very powerful tool and you don't have to limit yourself to the template provided by the core developers.

If you don't need environments, then don't use them, but I assure you some people will find it very useful.

OTHER TIPS

Yii2 documentation in WIP, but you should read this :

https://github.com/yiisoft/yii2/blob/master/docs/guide/apps-advanced.md#configuration-and-environments

You need to use yii init command to switch between these environments.

EDIT :

This new environment feature is more than just use different config file. You can use different folder structure, different entry script...etc

Personnaly I won't use this feature, I don't need it (I will use a different entry script as with Yii 1), but I think this is not useless.

I think you didn't get the real purpose of environments introduced in Yii2.

I'll try to explain what was the main purpose of adding environments to yii from the developers point of view on an example and hope you will really appreciate its usefulness.

Let's suppose for a moment that you are a team of developers (e.g. 5-7 person) working on mid-to-large project implemented in Yii. To effectively work on that project your team decides to use some CVS or SVN (e.g. GIT) and keep all the files of the project in repository in cloud for the whole team. That's de facto standard while working on mid-to-large projects in teams and nobody will resist that it's the only comfortable and easy way.

Ok, now let's suppose you use Yii 1.x or Yii2 with the approach of different entry scripts to differentiate between local (development) and production environments to connect to db or set some other environment specific configs. Everything is ok and working. But suppose your team members implemented something new on the project and you check out repository to work on updated version and you suddenly find out that your local config file (in this case entry script with config) is overwritten with other team member's file who pulled the changes to repository (because each of you is using your local machine db with other database name or OS, or config, or simply because your team uses one local development server db, but you are on vacation and can't use anything except your local machine).

So generally Yii2 environment adds more flexibility for using different environments each with it's own specific configurations while using also general (common) configs when working in teams on mid-to-large projects hence why the example in guide is given on advanced app project.

Surely you can overcome everything stated above with some solutions or .gitignore which is used by default to overcome the problem stated in Yii2 with environments. But:

  1. Why bother if everything is already done?

and

  1. It was just one little example of usefulness of Yii2 environments. More depends on the project and your imagination.

Overall Yii2 is great product. Not only it adds many new features to already great framework, but it also is more robust and flexible than Yii 1.x (despite the fact that Yii 1.x was already very robust).

As for Laravel or any other PHP framework, it really depends... Everyone will find his/her own favorite.

For those who are tired of copying files around, I created a useful script that you can run in background to keep the files in sync on your dev environment:

File sync-env-files.sh

#!/bin/bash

ENVIRONMENT_DIR="/var/www/example.com/environments/dev/"
DIR="/var/www/example.com/"

while [ true ]; do
    for envFile in `find $ENVIRONMENT_DIR -type f`
    do
        file=${envFile/$ENVIRONMENT_DIR/$DIR}
        if [ `stat -c "%Y" $file` -gt `stat -c "%Y" $envFile` ]; then
            #echo "copying modified file $file to $envFile"
            /bin/cp -f $file $envFile
        fi
    done
    sleep 2
done

Then run the script in background or add to cron with flock

nohup server/sync-env-files.sh >/dev/null 2>&1 &

I would like to mention in addition to @AngelCoding, since this question still gets seen, that I use the environments folder lots now and definitely see the point of it.

The very first things I do in any open source project is create one project for the code base on GitHub and then another, private, one on Bitbucket for the configuration, in other words the environments folder.

Having this folder has made it a lot easier for me to separate my configuration into a private repository.

So the environments folder has a lot of uses and really helps to separate configuration for easier usage even if it does not seem like it initially.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top