4

I'm building a website that has a "subdomain" called marketplace. so the url will be marketplace.sample.com. I'm using the Yii2 advanced application and I added the following code to my index.php located in frontend/web.

defined('MARKETPLACE') or define('MARKETPLACE', preg_match('/^marketplace/', $_SERVER['HTTP_HOST']) === 1 ? true : false);

This works on my environment, however, I just realized that the index.php file is in the .gitignore file in Yii2 because that file is created by the init script and so changes to it will be overwritten by running the init.

Anyway, so the question is: Where do I put this code so that it can be committed and shared with the rest of the dev team and make it to production when the code is pushed?

I tried to put this code in the common/config/params.php but then when I try to access the variable to determine which route to use I can't because the Yii app has not be initialized when the frontend/config/main.php file is run and I get an error that I am trying to access a property of a non-object.

/frontend/config/main.php

'defaultRoute' => MARKETPLACE ? 'marketplace' : 'site',

/frontend/config/main.php (with param instead)

'defaultRoute' => Yii::$app->params['marketplace'] ? 'marketplace' : 'site'

this second one gives the error that I am trying to access a property of a non-object.

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
paviktherin
  • 121
  • 2
  • 9
  • why not adding another file (env.php for example), and require it in index.php, thus keeping it out of .gitignore. – apoq Apr 16 '15 at 16:03
  • I can't add anything inside the index.php and have it shared with the other devs because it's being ignored. So I can't add a require in it for the same reason that I can't add the define that I have above. – paviktherin Apr 16 '15 at 16:44

2 Answers2

5

In the directory:

 common/config 

you can use bootstrap.php file for inserting the code you need. This file is executed in startup phase and is not indicated in .gitignore.

In this way you can assign the constant MARKETPLACE sure of propagate the code when you use the GIT

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Tamil Selvan C Apr 16 '15 at 17:34
  • This is ( i think ) a correct answer and not a critique or request clarification from an author. bootstrap.php is the first part of code executed, is not inside the .gitignore file and i think could be the right place to insert the necessary code. – ScaisEdge Apr 16 '15 at 17:50
  • The answer makes no sense to me. This might be due to the typos and illogical structure of your answer. You might want to consider improving your answer. – Mark Rotteveel Apr 16 '15 at 18:10
  • @scaisEdge Thank you so much! This is **exactly** what I needed. For others that aren't following this, what I did was added the code that I had placed in the /frontend/web/index.php file into the /common/config/bootstrap.php file instead. `defined('MARKETPLACE') or define('MARKETPLACE', preg_match('/^marketplace/', $_SERVER['HTTP_HOST']) === 1 ? true : false);` This allows for the code that determines which controller to set as the default route to work on all environments including production. – paviktherin Apr 17 '15 at 03:54
0

I don't know if this is the best practice for what you want to accomplish but you can provide a new init environment to ./init

The environments folder contains both a dev and prod folder that contain all the files that aren't version controlled and that are set on ./init (respectively for options 1) Development and 2) Production). A bit more on environment folders here.

So for example, lets say you want to create a "custom" version of the dev environment, and you want to modify the frontend entry script.

You would copy the environments/dev folder to environments/custom and customize the environments/custom/frontend/web/index.php file there.

Then add the following to environments/index.php :

'Custom' => [
        'path' => 'custom',
        'setWritable' => [
            'backend/runtime',
            'backend/web/assets',
            'frontend/runtime',
            'frontend/web/assets',
        ],
        'setExecutable' => [
            'yii',
        ],
        'setCookieValidationKey' => [
            'backend/config/main-local.php',
            'frontend/config/main-local.php',
        ],
    ],

Add and commit your changes and from here on out you should have a new 3) Custom option when you ./init and you and your devs can use that to initialize your applications.

PS: I haven't tested this but I think it should work (if not only minor tweaking should be necessary)

Community
  • 1
  • 1
Pomme.Verte
  • 1,782
  • 1
  • 15
  • 32
  • 1
    I think this would work, but it seems like "overkill" for what I needed to do. The answer was actually extremely simple. I just had to move the code from the /frontend/web/index.php into the /common/config/boostrap.php file. I will keep this in mind though, because it could come in very handy in the future. – paviktherin Apr 17 '15 at 03:59