php 开发指南

文档: https://developer.adobe.com/commerce/php/development/

此文档面向开发者,教你怎么开发 magento。与之相关的有开发者操作指南,后者偏向于教你如何搭建 magento 系统、配置系统、运维。

简介

Adobe Commerce 和 Magento 开源应用程序由模块、主题和语言包组成:

开发者 roadmap

di config 依赖注入配置

应用程序分以下阶段加载配置:

  1. 初始 ( app/etc/di.xml )
  2. 全局 ( <moduleDir>/etc/di.xml )
  3. 特定区域 ( <moduleDir>/etc/<area>/di.xml )

area 包括:

在引导期间,每个应用程序入口点都会为请求的区域加载适当的 di.xml 文件。

虚拟类型

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 
<virtualType name="moduleConfig" type="Magento\Core\Model\Config">
 
<arguments>
 
<argument name="type" xsi:type="string">system</argument>
 
</arguments>
 
</virtualType>
 
<type name="Magento\Core\Model\App">
 
<arguments>
 
<argument name="config" xsi:type="object">moduleConfig</argument>
 
</arguments>
 
</type>
 
</config>

这个配置文件,创建了一个名为 moduleConfig 的类,该类是 Magento\Core\Model\Config 的一个特定类型,即 type=system。然后把 Magento\Core\Model\Appconfig 参数配置为了 moduleConfig 类。

其中的 <type> 可以把特定参数传到构造函数中。具体支持:

示例:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Example\Type">
<arguments>
<!-- Pass simple string -->
<argument name="stringParam" xsi:type="string">someStringValue</argument>
<!-- Pass instance of Magento\Some\Type -->
<argument name="instanceParam" xsi:type="object">Magento\Some\Type</argument>
<!-- Pass true -->
<argument name="boolParam" xsi:type="boolean">1</argument>
<!-- Pass 1 -->
<argument name="intParam" xsi:type="number">1</argument>
<!-- Pass application init argument, named by constant value -->
<argument name="globalInitParam" xsi:type="init_parameter">Magento\Some\Class::SOME_CONSTANT</argument>
<!-- Pass constant value -->
<argument name="constantParam" xsi:type="const">Magento\Some\Class::SOME_CONSTANT</argument>
<!-- Pass null value -->
<argument name="optionalParam" xsi:type="null"/>
<!-- Pass array -->
<argument name="arrayParam" xsi:type="array">
<!-- First element is value of constant -->
<item name="firstElem" xsi:type="const">Magento\Some\Class::SOME_CONSTANT</item>
<!-- Second element is null -->
<item name="secondElem" xsi:type="null"/>
<!-- Third element is a subarray -->
<item name="thirdElem" xsi:type="array">
<!-- Subarray contains scalar value -->
<item name="scalarValue" xsi:type="string">ScalarValue</item>
<!-- and application init argument -->
<item name="globalArgument " xsi:type="init_parameter">Magento\Some\Class::SOME_CONSTANT</item>
</item>
</argument>
</arguments>
</type>
</config>

集成配置

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 
<type name="Magento\Framework\View\Element\Context">
 
<arguments>
 
<argument name="urlBuilder" xsi:type="object">Magento\Framework\Url</argument>
 
</arguments>
 
</type>
 
<type name="Magento\Backend\Block\Context">
 
<arguments>
 
<argument name="urlBuilder" xsi:type="object">Magento\Backend\Model\Url</argument>
 
</arguments>
 
</type>
 
</config>

Magento\Backend\Block\ContextMagento\Framework\View\Element\Context 的子类,其中的配置 Magento\Backend\Model\Url 优先级更高,会覆盖父类中的配置。

优化开发环境

  1. 使用最新的 PHP 8 版本:建议安装并使用最新受支持的 PHP 8 版本以提高性能。
  2. 使用 Percona 替代 MySQL 数据库:将 MySQL 数据库替换为 Percona 以获得更好的性能。
  3. 启用 PHP Opcache:确保安装并启用 PHP Opcache。
  4. 谨慎使用 Xdebug:Xdebug 默认关闭。仅在需要时启用此功能,因为它需要大量内存并会降低性能。对于 Magento,将 xdebug.max_nesting_level 配置设置为 200 或更高。同时,增加 PHP 可用的内存以提高 Xdebug 性能。
  5. 安装示例数据:如果需要示例数据,可以使用 Composer 或克隆存储库来安装。
  6. 关闭 CSS 和 JavaScript 的合并:为加快前端开发速度,关闭 CSS 和 JavaScript 的合并。
  7. 开启缓存:确保缓存已打开(这是默认行为)。一般来说,开发时只应关闭页缓存和块缓存,测试时应重新打开。
  8. 开启 Opcache 时间戳验证:在开发中,Opcache 时间戳验证应始终开启。因为在开启 Opcache 并关闭重新验证的情况下进行开发是不可能的,任何 PHP 修改都需要重置缓存。

以下章节需要整理: