使用php更新joomla配置文件

时间:2017-09-30 18:06:14

标签: php joomla3.0

我希望能够使用php在Joomla 3.x configuration.php文件中设置值。

我可以通过以下方式轻松获得价值:

$config = new JConfig();
$dbprefix = $config->dbprefix;

我已搜索但无法找到任何设置值的方法。我认为这是可能的,因为joomla做到了。我可以读取整个文件并使用php进行搜索和替换,但我希望Joomla有一个带有setter或类似的API,允许我这样做。

我搜索过并搜索过,但我找不到任何东西

帮助!

TA

我现在也尝试在JConfig中添加一个setter,但这似乎不起作用!

public function setTestvar($testvar) { 
  $this->ftp_user = $testvar; 
}

1 个答案:

答案 0 :(得分:1)

不幸的是我对Joomla并不熟悉!但是我快速浏览了GitHub上的源代码,似乎有一些功能可以在ConfigModelApplication::writeConfigFile()中执行你想要的Joomla\Registry\Registry实例。

此类在文件administrator/components/com_config/model/application.php

中定义

我假设您正在使用最新版本的Joomla!我没有对YMMV进行过测试。

以下是有问题的方法:

/**
 * Method to write the configuration to a file.
 *
 * @param   Registry  $config  A Registry object containing all global config data.
 *
 * @return  boolean  True on success, false on failure.
 *
 * @since   2.5.4
 * @throws  RuntimeException
 */
private function writeConfigFile(Registry $config)
{
    jimport('joomla.filesystem.path');
    jimport('joomla.filesystem.file');

    // Set the configuration file path.
    $file = JPATH_CONFIGURATION . '/configuration.php';

    // Get the new FTP credentials.
    $ftp = JClientHelper::getCredentials('ftp', true);

    $app = JFactory::getApplication();

    // Attempt to make the file writeable if using FTP.
    if (!$ftp['enabled'] && JPath::isOwner($file) && !JPath::setPermissions($file, '0644'))
    {
        $app->enqueueMessage(JText::_('COM_CONFIG_ERROR_CONFIGURATION_PHP_NOTWRITABLE'), 'notice');
    }

    // Attempt to write the configuration file as a PHP class named JConfig.
    $configuration = $config->toString('PHP', array('class' => 'JConfig', 'closingtag' => false));

    if (!JFile::write($file, $configuration))
    {
        throw new RuntimeException(JText::_('COM_CONFIG_ERROR_WRITE_FAILED'));
    }

    // Attempt to make the file unwriteable if using FTP.
    if (!$ftp['enabled'] && JPath::isOwner($file) && !JPath::setPermissions($file, '0444'))
    {
        $app->enqueueMessage(JText::_('COM_CONFIG_ERROR_CONFIGURATION_PHP_NOTUNWRITABLE'), 'notice');
    }

    return true;
}

希望这会有所帮助:)