paviktherin
paviktherin

Reputation: 131

Where do I put global Variables in Yii2 similar to YII_DEBUG and YII_ENV

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.

Upvotes: 4

Views: 6427

Answers (2)

Pomme.Verte
Pomme.Verte

Reputation: 1792

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)

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133360

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

Upvotes: 5

Related Questions