Magento开发系列之十二 默认系统配置

本节并没有太多新内容,更多的是对前面有关系统后台配置的补充。在我们创建新的系统配置路径时,Magento并没有存储默认值,甚至对于一些系统默认配置也是如此,这点可以通过查看core_config_data表来进行验证。

mysql> select * from core_config_data;
+-----------+---------+---------+----------------------------+--------+
| config_id | scope   | scope_id| path                       | value  |
+-----------+---------+---------+----------------------------+--------+
|         1 | default |        0| general/region/display_all | 1      |
... ...

这张表仅存储在后台或基它程序中明确设置的值,而如果请求一个没有进行这个设置的系统配置值的话,Magento会到全局配置文件中去查看默认值。虽然不要求这么做,但为自添加配置变量设置一个默认值是一个不错的习惯。这样做很简单,也防止在获取到空值时产生一些意想不到的效果。

上面提到默认值存放在全局配置文件中,这可能与很多的想法大相径庭,因为大家可能会认为这默认值会存储在用于配置后台的system.xml文件中。为什么要这么做其实大可不必深究,我们可以理解为配置文件中存储常用的值,而stystem.xml中存储着用于修改这些值的界面的配置。

在模块配置文件config.xml里添加一个<default />代码块(如app/code/local/Packagename/Modulename/etc/config.xml)

<config>
    <!-- ... -->
    <default>
    </default>
    <!-- ... -->
</config>

这就是我们用于存储默认值的最上级节点,接下来将配置路径转化成XML格式的树形节点,比如我们为以下配置路径设置默认值:

design/header/welcome

那么在config.xml的代码就会是这样:

<config>
    <!-- ... -->
    <default>
        <design>

<header>
                <welcome>Default welcome msg!</welcome>
            </header>

        </design>
    </default>
    <!-- ... -->
</config>

应用了这个配置后,请求design/header/welcome时,如果没有设定值,就会返回”Default welcome msg!”。这个例子是基本系统默认的配置,我们业看看design/header/welcome的真实配置(文件地址:app/code/core/Mage/Page/etc/config.xml):

<default>
    <design>
        <head translate="default_description" module="page">
            <default_title>Magento Commerce</default_title>
            <default_description>Default Description</default_description>
            <default_keywords>Magento, Varien, E-commerce</default_keywords>
            <default_robots>*</default_robots>
            <default_media_type>text/html</default_media_type>
            <default_charset>utf-8</default_charset>
        </head>

<header translate="welcome" module="page">
            <logo_src>images/logo.gif</logo_src>
            <logo_alt>Magento Commerce</logo_alt>
            <logo_src_small>images/logo.gif</logo_src_small>
            <welcome>Default welcome msg!</welcome>
        </header>

<footer translate="copyright" module="page">
            <copyright>&amp;copy; 2015 Magento Demo Store. All Rights Reserved.</copyright>
        </footer>

    </design>
    <system>
        <media_storage_configuration>
            <allowed_resources>
                <site_favicons>favicon</site_favicons>
            </allowed_resources>
        </media_storage_configuration>
    </system>
</default>

这是所有design/*的默认配置,和我们例子中不同的时这里有一个translate属性:

<head translate="default_description" module="page">

translate和module属性告诉系统哪些节点需要被translate,以及使用哪个模块的Data Helper来进行这一操作。在上例中,welcome节点将被转化为:

Mage::helper('page')->__(...);

前面我们也提到过,如果调用helper类时URI没有传入第二部分,会默认使用data,也就是说下面两句代码是一样的:

Mage::helper('page')->__(...);
Mage::helper('page/data')->__(...);

如果想要转化多个子节点,可以在名称之间用逗号进行分隔,如:

<example translate="foo,baz,bar" module="page">

写在后面

本系列更新到此结束,一共十二篇,大多数代码并非出自笔者之手,而是来自Magento资深大师Alan Storm,笔者只是用自己的理解丰富了一下内容,以有助于国内Magento学习者更为有效地掌握Magento开发相关知识。本系列的终结只是一个阶段性的符号,Magento作为一套强大的电商系统还有很值得探讨和学习的地方,需要大家共同努力去研究和分享,让我们一起努力吧!

安装Magento系统要求

安装Magento需要满足一些系统最低配置要求,详情可访问以下页面: http://magento.com/resources/system-requirements

Supported Operating Systems:

  • Linux x86, x86-64

Supported Web Servers:

  • Apache 1.3.x
  • Apache 2.0.x
  • Apache 2.2.x
  • Nginx (starting from Magento 1.7 Community and 1.12 Enterprise versions)

Supported Browsers:

  • Microsoft Internet Explorer 7 and later
  • Mozilla Firefox 3.5 and later
  • Apple Safari 5 and later on Mac only
  • Google Chrome 7 and later
  • Adobe Flash browser plug-in should be installed

PHP Compatibility:

  • 5.2.13 – 5.3.24; or 5.4.x with a patch for CE or EE (the patch is listed as PHP 5.4 Compatibility in the EE support portal)
  • Required extensions:
    • PDO_MySQL
    • simplexml
    • mcrypt
    • hash
    • GD
    • DOM
    • iconv
    • curl
    • SOAP (if Webservices API is to be used)
  • Safe_mode off
  • Memory_limit no less than 256Mb (preferably 512)

MySQL:

  • EE 1.13.0.0 or later: MySQL 5.0.2 or later
  • EE 1.12.0.2 or earlier: MySQL 4.1.20 or later
  • CE (all versions): MySQL 4.1.20 or later
  • Because Magento CE and EE require transactional database support, the InnoDB engine must be used with any version of MySQL. Consult THE MYSQL DOCUMENTATION for more information on InnoDB engine support and installation.

Redis NoSQL (optional for CE 1.8 and later, EE 1.13 and later)

  • redis-server version 2.6.9 or later
  • phpredis version 2.2.2 or later

SSL:

  • A valid security certificate is required to work over a secure channel (HTTPS) from the Magento Admin Panel or when submitting form data from the storefront. Self-signed SSL certificates are not supported.

Server – hosting – setup:

  • Ability to run scheduled jobs (crontab) with PHP 5
  • Ability to override options in .htaccess files

由于参数较多,为了便于检测,Magento官方为我们提供了一个检测工具:http://www.magentocommerce.com/_media/magento-check.zip

将所下载文件解压缩并将文件上传到到需要安装Magento的目录下,然后在相关目录url地址后添加/magento-check.php进行访问,文件使用的是短标记<?而非常用的<?php,如果系统不支持short tags,则需要修改起始标签。magento-check.php文件的内容如下:

<?
extension_check(array(
‘curl’,
‘dom’,
‘gd’,
‘hash’,
‘iconv’,
‘mcrypt’,
‘pcre’,
‘pdo’,
‘pdo_mysql’,
‘simplexml’
));

function extension_check($extensions) {
$fail = ”;
$pass = ”;

if(version_compare(phpversion(), ‘5.2.0’, ‘<‘)) {
$fail .= ‘<li>You need<strong> PHP 5.2.0</strong> (or greater)</li>’;
}
else {
$pass .='<li>You have<strong> PHP 5.2.0</strong> (or greater)</li>’;
}

if(!ini_get(‘safe_mode’)) {
$pass .='<li>Safe Mode is <strong>off</strong></li>’;
preg_match(‘/[0-9]\.[0-9]+\.[0-9]+/’, shell_exec(‘mysql -V’), $version);

if(version_compare($version[0], ‘4.1.20’, ‘<‘)) {
$fail .= ‘<li>You need<strong> MySQL 4.1.20</strong> (or greater)</li>’;
}
else {
$pass .='<li>You have<strong> MySQL 4.1.20</strong> (or greater)</li>’;
}
}
else { $fail .= ‘<li>Safe Mode is <strong>on</strong></li>’; }

foreach($extensions as $extension) {
if(!extension_loaded($extension)) {
$fail .= ‘<li> You are missing the <strong>’.$extension.'</strong> extension</li>’;
}
else{ $pass .= ‘<li>You have the <strong>’.$extension.'</strong> extension</li>’;
}
}

if($fail) {
echo ‘<p><strong>Your server does not meet the following requirements in order to install Magento.</strong>’;
echo ‘<br>The following requirements failed, please contact your hosting provider in order to receive assistance with meeting the system requirements for Magento:’;
echo ‘<ul>’.$fail.'</ul></p>’;
echo ‘The following requirements were successfully met:’;
echo ‘<ul>’.$pass.'</ul>’;
} else {
echo ‘<p><strong>Congratulations!</strong> Your server meets the requirements for Magento.</p>’;
echo ‘<ul>’.$pass.'</ul>’;

}
}
?>

检测通过将显示如下提示:

Congratulations! Your server meets the requirements for Magento.

  • You have PHP 5.2.0 (or greater)
  • Safe Mode is off
  • You have MySQL 4.1.20 (or greater)
  • You have the curl extension
  • You have the dom extension
  • You have the gd extension
  • You have the hash extension
  • You have the iconv extension
  • You have the mcrypt extension
  • You have the pcre extension
  • You have the pdo extension
  • You have the pdo_mysql extension
  • You have the simplexml extension