1

So basically I am designing my PHP based website to a newer and more supported framework. Currently my website in written in Kohana 2.3.4 and I am moving to Yii2 framework. The biggest problem I am facing is the difference is that on my Kohana Site, I used constants for all my HTML for the case of translating but now I have no problem since I can just use: Yii::t()

Though I have solved this problem I needed to know how to pass my Settings Array to all actions on every controller. The settings are loaded from my SettingsQuery model and should be accessed from all the views in the app ie. Something like global constants. I have tried a few tips I got from my research but nothing is working as the scenario is a bit different from this and this.

I have tried a way to implement this way but it works only when I have static values for my settings. What is the best way to do this?

Community
  • 1
  • 1
Zack
  • 1,527
  • 2
  • 20
  • 32

1 Answers1

4

The two possible places I know to declare global constants in Yii 2.0 are :

  1. Your application Entry Scripts for global variables to which hole processes may depends. Maybe for serious init settings like which modules to activate and what components to disable.
  2. [your app]/config/params.php file which I thing is the proper place for placing your global settings because that's why it was built for. If you open that file, you'll already find this inside :

    return [
        'adminEmail' => 'admin@example.com',
    ];
    

    This information is accessible any where in your application within this :

    $admin_email = \Yii::$app->params['adminEmail'] ;   
    

    So in order to relate it to your SettingsQuery model, you will need to create a Bootstrap class which will need to create an instance from your model and use it to set the global constants when bootstrapping your app. @arogachev have a great example about how to create a Bootstrap class in this StackOverflow question.

Community
  • 1
  • 1
Salem Ouerdani
  • 7,596
  • 3
  • 40
  • 52
  • Let me try this out and tell you how it goes. – Zack Jul 26 '15 at 16:21
  • how do you think I could do this for module params? I want to define variables only to a certain module. I havent managed to do it since I am still reading on implementing the application bootstrap. – Zack Jul 27 '15 at 13:21
  • sorry i've seen your comment when using mobile but forget to answer it when reaching my laptop. Modules have an `init()` method which seems a good place to do so. [here is how it works - from docs](http://www.yiiframework.com/doc-2.0/guide-structure-modules.html#module-classes). – Salem Ouerdani Sep 06 '15 at 13:54