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

自作多情 提交于 2020-01-03 16:47:42

问题


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.


回答1:


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




回答2:


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)



来源:https://stackoverflow.com/questions/29678700/where-do-i-put-global-variables-in-yii2-similar-to-yii-debug-and-yii-env

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!