Set global variables for all controllers in Kohana 2.3.4

不问归期 提交于 2019-12-08 09:07:32

问题


Is the proper way to make a few variables available to all my controllers to add a MY_Controller.php file in my /application/libraries/ folder (shown in the docs here)?

I'm working in Kohana 2.3.4 and wondering if there are any better ways to do it, or is this the only recommended method?

Being new to OOP, can you link me to any examples?

I've heard the right answer is to add the vars to your $config[], trying to get more details.


回答1:


The proper way is to make a custom config file (application/config/foobar.php), and access the data with Kohana::config('foobar.key').

The code igniter way is completely wrong and inappropriate.

See http://docs.kohanaphp.com/core/kohana#methods_config




回答2:


How does this feel then:

[bootstrap.php]

Kohana::$config->attach(new Kohana_Config_File('global'));

And then, create a new file under application/config called global.php

In it, put (for example):

return (array ('MyFirstVar' => 'Is One',
               'MySecondVar' => 'Is Two'));

Anywhere in your code, access these variables with

Kohana::config ('global.MyFirstVar');

As you can see, 'global.' is used to access these variables; the reason for that is that you attached the global.php config file at the beginning.

Was this what you meant? :-)



来源:https://stackoverflow.com/questions/1969698/set-global-variables-for-all-controllers-in-kohana-2-3-4

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