【问题标题】:Easiest way to access Phalcon config values in views?在视图中访问 Phalcon 配置值的最简单方法是什么?
【发布时间】:2014-01-27 14:37:27
【问题描述】:

我在 ini 文件中有一个部分,其中包含一些全球使用的社交链接,例如:

[social]
fb = URL
twitter = URL
linkedin = URL

访问这些变量的最简单方法是什么,或者有更好的方法来组织这些全局变量?

【问题讨论】:

标签: php phalcon volt


【解决方案1】:

如果您在初始化/引导您的应用程序时读取配置文件并将其存储在 DI 容器中,那么您的应用程序的每个部分都可以通过它访问它。

示例 - 引导

$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);

现在您可以在控制器中访问它:

echo $this->config->social->twitter;

通过 Volt 观看:

{{ config.social.twitter }}

您始终可以通过基本控制器在视图中设置配置的特定部分。

class ControllerBase() 
{
    public function initialize()
    {
        parent::initialize();

        $this->view->setVar('social', $this->config->social);
    }
}

然后通过您的视图访问该变量:

{{ social.twitter }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    相关资源
    最近更新 更多