我在ini文件中有一个部分,其中包含一些全球使用的社交链接,例如:
[social]
fb = URL
twitter = URL
linkedin = URL
Run Code Online (Sandbox Code Playgroud)
访问这些的最简单方法是什么,还是有更好的方法来组织这些全局变量?
Nik*_*los 26
如果您在初始化/引导应用程序时读取配置文件并将其存储在DI容器中,那么可以通过应用程序的每个部分访问它.
示例 - bootstrap
$di = new \Phalcon\DI\FactoryDefault();
$configFile = ROOT_PATH . '/app/var/config/config.ini';
// Create the new object
$config = new \Phalcon\Config\Adapter\Ini($configFile);
// Store it in the Di container
$di->set('config', $config);
Run Code Online (Sandbox Code Playgroud)
现在您可以在控制器中访问它:
echo $this->config->social->twitter;
Run Code Online (Sandbox Code Playgroud)
Volt观点:
{{ config.social.twitter }}
Run Code Online (Sandbox Code Playgroud)
您始终可以通过基本控制器在视图中设置配置的特定部分.
class ControllerBase()
{
public function initialize()
{
parent::initialize();
$this->view->setVar('social', $this->config->social);
}
}
Run Code Online (Sandbox Code Playgroud)
然后通过您的视图访问该变量:
{{ social.twitter }}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11576 次 |
| 最近记录: |