Magento基础知识,请求流程,标准和最佳实践

Nowadays, companies are migrating their businesses online more and more. They’ve chosen to direct their attention to online communication in order to stay closer to their potential buyers, to make use of social websites to inform people about their products/services and, finally, to find a workaround that provides people with a buying process directly from home, work office or any place with an Internet connection.

如今,公司正在越来越多地在线迁移其业务。 他们选择将注意力转移到在线交流上,以便与潜在买家保持更近的距离,利用社交网站向人们介绍他们的产品/服务,最后找到一种可以为人们提供购买过程的变通方法。直接从家里,办公室或任何有Internet连接的地方。

One of the solutions for e-commerce that gained trust from lots of companies is Magento. It supports small businesses, but also scales to larger sizes.

Magento是获得许多公司信任的电子商务解决方案之一。 它支持小型企业,但也可以扩展到更大的规模。

Alongside the expansion of e-commerce came across the need for experts to maintain and customize functionalities for their specific shop infrastructure.

除了电子商务的扩展之外,还需要专家来维护和定制其特定商店基础结构的功能。

In this article we will discuss the initial step that needs to be accomplished by a developer in order to get familiar with Magento’s structure and be ready to start adding custom functionality.

在本文中,我们将讨论开发人员需要完成的第一步,以便熟悉Magento的结构并准备开始添加自定义功能。

Magento的基础知识 (Magento’s Basics)

A free version of Magento Community Edition can be downloaded from here, the official Magento website.

可以从这里 (Magento官方网站)下载免费版本的Magento社区版。

Assuming we have already configured a virtual host in a local environment and extracted Magento in the desired folder, before firing the installer, file permissions needs to be set correctly. Here are the steps:

假设我们已经在本地环境中配置了虚拟主机,并在所需的文件夹中提取了Magento,则在触发安装程序之前,需要正确设置文件权限。 步骤如下:

  • set all directories and subdirectories from the application to 775

    将应用程序中的所有目录和子目录设置为775

  • set all files to 644

    将所有文件设置为644

  • set directory app/etc/ and all its files and subdirectories to 777

    将目录app / etc /及其所有文件和子目录设置为777

  • set directory var/ and all its files and subdirectories to 777

    将目录var /及其所有文件和子目录设置为777

  • set directory media and all its files and subdirectories to 777

    将目录媒体及其所有文件和子目录设置为777

In a linux system you can achieve all this by typing the following commands in your Magento folder:

在linux系统中,您可以通过在Magento文件夹中键入以下命令来实现所有这些目的:

find . -type d -exec chmod 775 {} \;
find . -type f – exec chmod 644 {} \;
chmod 777 -R app/etc/
chmod 777 -R var/
chmod 777 -R media/`

After the installer is done working, file permissions from app/etc should be changed back – directories to 775 and files to 644, for security reasons.

安装程序完成工作后,出于安全原因,应将来自app/etc文件权限改回–目录为775,文件为644。

代码池 (Code pools)

Modules are located in app/code/ in the existing code pools: core, community (deprecated) and local.

模块位于现有代码池的app/code/中:核心,社区(不建议使用)和本地。

Each module has a corresponding configuration in an .xml file in app/etc/modules/ and has defined a syntax like <codePool>local</codePool> from which Mage_Core_Model_ConfiggetModuleDir() will point to the module’s path.

每个模块在app/etc/modules/中的.xml文件中都有相应的配置,并定义了类似<codePool>local</codePool>的语法, Mage_Core_Model_ConfiggetModuleDir()将从该语法指向该模块的路径。

模块结构 (Module structure)

(Block)

The files in this folder inherit or load data and transfer it to the templates from your theme (.phtml files).

此文件夹中的文件继承或加载数据,并将其从您的主题(.phtml文件)传输到模板。

控制器 (controllers)

Controllers represent the business logic, containing specific actions that are matched for a given request (dispatch(), preDispatch(), postDispatch() methods) and delegate commands to the other parts of the system.

控制器代表业务逻辑,包含与给定请求(dispatch(),preDispatch(),postDispatch()方法)匹配的特定操作,并将命令委托给系统的其他部分。

帮手 (Helper)

In the Helper directory you can create class files with utility methods, parsing methods and other generally helpful methods for your store which are commonly used by the whole system. Methods declared in helpers can be called from any template file or block, model or controller. Magento is returning a singleton instance for helper classes.

在Helper目录中,您可以使用实用程序方法,解析方法和其他通常对系统有用的方法来创建类文件,这些方法是整个系统常用的。 可以从任何模板文件或块,模型或控制器中调用在助手中声明的方法。 Magento正在返回帮助器类的单例实例。

控制者 (Controller)

In this directory routes can be defined, used to match module-controller-action based on the request, template layers for controller classes, etc. For an example, see the “Paypal” module.

在此目录中,可以定义路由,用于根据请求匹配模块-控制器-操作,控制器类的模板层等。有关示例,请参见“ Paypal”模块。

模型 (Model)

Usually contains classes that correspond to database tables. Models can be used as regular models that use two resource classes (one for reading and one for writing) to communicate with the database, resource models used to prepare query data for database communication, service models used as standalone classes that define a process flow, helper models used as standalone classes with individual methods for computing more complex algorithms than regular Helpers, and so on.

通常包含与数据库表相对应的类。 模型可以用作常规模型,这些模型使用两种资源类(一种用于读取,一种用于写入)与数据库进行通信,资源模型用于为数据库通信准备查询数据,服务模型用作定义流程的独立类,辅助程序模型用作独立类,具有单独的方法来计算比常规辅助程序更复杂的算法,等等。

等等 (etc)

Contains module behavior configurations. Each module must have at least a config.xml file and declare paths for models, blocks, helpers, routers, etc.

包含模块行为配置。 每个模块必须至少有一个config.xml文件,并声明模型,块,助手,路由器等的路径。

sql (sql)

Each module can come along with sql installers to prepare database entities. Installers have a group name defined in each module/etc/config.xml and each sql installer has a version for order iteration purposes.

每个模块可以与sql安装程序一起提供以准备数据库实体。 安装程序在每个module/etc/config.xml定义了一个组名,每个sql安装程序都有一个用于订单迭代目的的版本。

数据 (data)

Almost the same functional statement as sql installers, but with a different scope: will not create/update table schemes and attributes, instead it will update existing records or populate database tables with default records.

几乎与sql安装程序的功能相同,但作用域不同:将不会创建/更新表方案和属性,而是将更新现有记录或使用默认记录填充数据库表。

doc (doc)

A folder dedicated to documentation and module explanation.

一个专用于文档和模块说明的文件夹。

模板和布局 (Templates and Layout)

The layout in Magento is kept in app/design/ and has a well defined structure for default and custom theming. Loading hierarchy within its priority and file existence starts with the custom theme. If not found, it will search the .phtml file in default theme and if still not found, the last search path will be the base theme.

Magento中的布局保留在app/design/并且具有用于默认和自定义主题的定义明确的结构。 在优先级和文件存在范围内加载层次结构始于自定义主题。 如果找不到,它将以默认主题搜索.phtml文件,如果仍然找不到,最后的搜索路径将为基本主题。

Magento establishes the theming structure in high-level areas such as adminhtml (system administration templates), frontend (the visible template for an end-user), installer (templates for the helper system that will automatically configure the shop).

Magento在高级区域中建立主题结构,例如adminhtml(系统管理模板),前端(最终用户的可见模板),安装程序(将自动配置商店的帮助程序系统的模板)。

Each theme has a folder ‘layout’ with .xml files (usually each module has its own referred layout configuration file) that defines a block of content for a controller action.

每个主题都有一个带有.xml文件的文件夹“布局”(通常每个模块都有其自己引用的布局配置文件),该文件定义了控制器动作的内容块。

The custom theme may also contain a folder called ‘locales’ that will keep a file ‘translate.csv’ where all the custom translations are kept or even override existing ones already defined in standard Magento.

自定义主题可能还包含一个名为“ locales”的文件夹,该文件夹将保留一个文件“ translate.csv”,所有自定义翻译都将保留在该文件中,甚至覆盖标准Magento中已定义的现有翻译。

皮肤和JavaScript (Skin and Javascript)

The CSS files, images, and custom JS are located in the directory skin/ and implement the same theming structure and loading priority as for Templates and Layout (skin/area_name/package_name – a custom namespace can be defined or base or default/theme_name).

CSS文件,图像和自定义JS位于目录skin/并实现与模板和布局相同的主题结构和加载优先级( skin/area_name/package_name –可以定义自定义名称空间,也可以定义basedefault/theme_name ) 。

类命名约定 (Class naming conventions)

Magento applies the basic, albeit outdated Zend class naming convention and uses Varien_Autoload::register() to autoload classes by replacing the ‘_’ in the name with a directory separator to find the inclusion path to the file. This is very primitive, but due to backwards compatibility could not be solved sooner – Magento 2 will use ZF2, modern PHP and namespaces.

Magento应用了基本的Zend类命名约定(尽管已过时Varien_Autoload::register() ,并使用Varien_Autoload::register()通过用目录分隔符替换名称中的'_'来自动加载类,以查找文件的包含路径。 这是非常原始的,但是由于向后兼容性无法尽快解决– Magento 2将使用ZF2,现代PHP和名称空间。

请求流程 (Request Flow)

Let’s see how a Magento application handles a request starting from the main index.php file.

让我们看看Magento应用程序如何从主index.php文件开始处理请求。

应用程序初始化 (Application Initialization)

1) If you use Nginx, you should follow the configuration guide. If you’re on Apache, these are the rewrite rules for .htaccess from the main application directory that shouldn’t be missing.

1)如果使用Nginx,则应遵循配置指南 。 如果您使用的是Apache,则这些是主应用程序目录中.htaccess的重写规则,不应该丢失。

Rewrite rules for skin, media, JS directories to allow loading files and, if not found, to return 404. Everything else is rewritten to index.php:

重写皮肤,媒体,JS目录的规则,以允许加载文件,如果找不到,则返回404。其他所有内容都重写为index.php:

<IfModule mod_rewrite.c>

############################################
## enable rewrites

    Options +FollowSymLinks
    RewriteEngine on

############################################
## always send 404 on missing files in these folders

    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/

############################################
## never rewrite for existing files, directories and links

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l

############################################
## rewrite everything else to index.php

    RewriteRule .* index.php [L]

</IfModule>

2) These are the steps the application follows when initialized:

2)这些是应用程序在初始化时遵循的步骤:

  • run file index.php where app/Mage.php is included

    运行文件index.php,其中包含app/Mage.php

  • call Mage::run($mageRunCode, $mageRunType)

    调用Mage::run($mageRunCode, $mageRunType)

  • in ‘run’, we load Varien Events Collection, the class Mage_Core_Model_Config and the class Mage_Core_Model_App

    在“运行”中,我们加载Varien Events Collection,类Mage_Core_Model_Config和类Mage_Core_Model_App

  • Mage_Core_Model_App::run() is triggered

    Mage_Core_Model_App::run()被触发

  • load configuration files from app/etc/modules

    app/etc/modules加载配置文件

  • load configuration xml files inside modules app/code/{code pool}/{module name}/etc/config.xml

    将配置xml文件加载到模块app/code/{code pool}/{module name}/etc/config.xml

  • initialize current store

    初始化当前商店

  • initialize request object

    初始化请求对象

  • run sql and data installers

    运行SQL和数据安装程序

  • update/create versions of modules in core_resource

    更新/创建core_resource中的模块版本

  • Front Controller dispatch – Mage_Core_Controller_Varien_Front::dispatch();

    前端控制器分派– Mage_Core_Controller_Varien_Front::dispatch();

  • collect routers from config by Area and Route Code

    通过区域和路由代码从配置中收集路由器

  • use routes to match Controller

    使用路由来匹配Controller

  • if a controller and action will be matched, the controller file will be included, instantiated and action method will be called

    如果控制器和动作将匹配,则将包含控制器文件,实例化并调用动作方法

  • action method will instantiate and call methods on models, depending on the request

    动作方法将实例化并在模型上调用方法,具体取决于请求

  • the Varien Action Controller will call a Layout Object

    Varien动作控制器将调用布局对象

  • this Layout Object will create a list of Block objects that are valid for this request, based on some request variables and system properties (also known as “handles”)

    此布局对象将基于一些请求变量和系统属性(也称为“句柄”)创建对此请求有效的Block对象的列表

  • template files (.phtml) are assigned in the layout xml file for each block.

    在布局xml文件中为每个块分配了模板文件(.phtml)。

  • _prepareLayout() method is called where, based on action layout xml structure, specific blocks are instantiated within associated .phtml files

    _prepareLayout()方法,其中根据操作布局xml结构在关联的.phtml文件中实例化特定的块

  • _toHtml() method is triggered to generate html

    触发_toHtml()方法以生成html

  • output html

    输出HTML

前置控制器 (Front controller)

1) the properties and role of the Front Controller:

1)前端控制器的属性和作用:

  • directory path: app/code/core/Mage/Core/Controller/Varien/Front.php

    目录路径: app/code/core/Mage/Core/Controller/Varien/Front.php

  • base role: receive all requests from the browser and return HTML code

    基本角色:接收来自浏览器的所有请求并返回HTML代码

  • Front controller uses routes of the system to match a controller class and its action method

    前端控制器使用系统的路由来匹配控制器类及其动作方法

  • default Magento routers are: Standard (class Mage_Core_Controller_Varien_Router_Standard), Admin (class Mage_Core_Controller_Varien_Router_Admin), Default (class Mage_Core_Controller_Varien_Router_Default)

    默认的Magento路由器是:标准( Mage_Core_Controller_Varien_Router_Standard ),管理员( Mage_Core_Controller_Varien_Router_Admin ),默认( Mage_Core_Controller_Varien_Router_Default )

Here’s is an example of a defined route in the Customer module’s /etc/config.xml:

这是客户模块的/etc/config.xml定义的路由的示例:

<routers>
    <customer>
        <use>standard</use>
        <args>
            <module>Mage_Customer</module>
            <frontName>Customer</frontName>
        </args>
    </customer>
</routers>

2) events that Front Controller triggers:

2)Front Controller触发的事件:

  • controller_front_init_before

    controller_front_init_before

  • controller_front_init_routers

    controller_front_init_routers

  • controller_front_send_response_before

    controller_front_send_response_before

  • controller_front_send_response_after

    controller_front_send_response_after

网址重写 (URL rewrites)

Magento中的URL结构/处理。 (URL structure/processing in Magento.)

A link in Magento has the below structure:

Magento中的链接具有以下结构:

https://user:password@host:443/{base_path}/{base_script}/{storeview_path}/{module_front_name}/{controller_name}/{action_name}/{param1}/{value1}?{ query_param=query_value#fragment}

https://user:password@host:443/{base_path}/{base_script}/{storeview_path}/{module_front_name}/{controller_name}/{action_name}/{param1}/{value1}?{ query_param=query_value#fragment}

  • user:password@host:443/{base_path}/{base_script}: the path to the Script file which runs Magento. Usually, it is an index.php file or a path that uses access rewrite rules to index.php

    user:password@host:443/{base_path}/{base_script} :运行Magento的脚本文件的路径。 通常情况下,它是一个index.php文件或路径使用访问重写规则来index.php

  • {storeview_path}: store view code will be used if the website has a multi-store-view model (e.g. multi-language support that is marked in the url)

    {storeview_path} :如果网站具有多商店视图模型(例如,URL中标记的多语言支持),则将使用商店视图代码

  • {module_front_name}/{controller_name}/{action_name}: the path to match a module controller and action method

    {module_front_name}/{controller_name}/{action_name} :匹配模块控制器和操作方法的路径

  • {param1}/{value1}: name and value of a parameter sent by url

    {param1}/{value1} :URL发送的参数的名称和值

  • ?query_param=query_value#fragment: parameters query sent via the standard GET request

    ?query_param=query_value#fragment :通过标准GET请求发送的参数查询

The above link will traverse through the following files, each contributing to match a schema for url and call the correct module-controller-action to output content:

上面的链接将遍历以下文件,每个文件都与URL的模式匹配,并调用正确的module-controller-action来输出内容:

  • app/Mage.php (Mage::app()->run())

    app/Mage.php (Mage::app()->run())

  • app/code/core/Mage/Core/Model/App.php

    app/code/core/Mage/Core/Model/App.php

  • Init and Dispatch controller $this->getFrontController()->dispatch();

    初始化和调度控制器$this->getFrontController()->dispatch();

  • app/code/core/Mage/Core/Controller/Varien/Front.php

    app/code/core/Mage/Core/Controller/Varien/Front.php

  • choose the defined router from config.xml to match module-controller-action $router->match($this->getRequest())

    config.xml选择已定义的路由器以匹配模块控制器动作$router->match($this->getRequest())

    • app/code/core/Mage/Core/Controller/Varien/Router/Admin.php

      app/code/core/Mage/Core/Controller/Varien/Router/Admin.php

    • app/code/core/Mage/Core/Controller/Varien/Router/Standard.php

      app/code/core/Mage/Core/Controller/Varien/Router/Standard.php

    • app/code/core/Mage/Core/Controller/Varien/Router/Default.php

      app/code/core/Mage/Core/Controller/Varien/Router/Default.php

  • app/code/core/Mage/Core/Controller/Varien/Action.php

    app/code/core/Mage/Core/Controller/Varien/Action.php

  • call Action method (example: indexAction())

    调用Action方法(例如: indexAction() )

URL重写过程 (URL rewrite process)

Url is rewritten in the following cases:

在以下情况下,URL会被重写:

  • core url rewrite: having priority over routers, will try to translate a custom request path to standard module/controller/action path based on the defined rows in database (core_url_rewrite table)

    核心网址重写:优先于路由器,将尝试根据数据库中定义的行将自定义请求路径转换为标准module/controller/action路径( core_url_rewrite表)

  • module name rewrite: a front name will be defined in a module’s config.xml to rewrite the actual path and name (example: Mage_Customer will be matched by url through using its defined front name ‘customer’)

    重写模块名称:将在模块的config.xml定义一个前端名称,以重写实际的路径和名称(例如: Mage_Customer将使用其定义的前端名称“ customer”与url匹配)

  • custom routers will rewrite the url (mostly used for SEO purposes): for the default Magento an example will be the CMS module that will match a url-identifier like homepage.html to module ‘Mage_Cms’, controller ‘Page’, action ‘View’ and a parameter id that will match the cms entity row in the database.

    自定义路由器将重写url(主要用于SEO):对于默认的Magento,将是CMS模块,该模块会将url-identifier(如homepage.html与模块“ Mage_Cms”,控制器“ Page”,操作“ View”匹配'和将与数据库中的cms实体行匹配的参数ID。

标准和最佳实践 (Standards and Best Practices)

样本配置xml (Sample configuration xml)

This is a sample of module config.xml (path: {base_app_folder}/app/local/{module_name}/etc/config.xml) that includes the main configuration:

这是模块config.xml的示例(路径: {base_app_folder}/app/local/{module_name}/etc/config.xml ),其中包括主要配置:

<?xml version="1.0"?>
<config>
    <modules>
        <{{localnamespaceC}}_{{modulenameC}}>
            <version>0.1.0</version>
        </{{localnamespaceC}}_{{modulenameC}}>
    </modules>
    <global>
        <models>
            <{{localnamespaceL}}_{{modulenameL}}>
                <class>{{localnamespaceC}}_{{modulenameC}}_Model</class>
            </{{localnamespaceL}}_{{modulenameL}}>
        </models>
        <resources>
            <{{localnamespaceL}}_{{modulenameL}}_setup>
                <setup>
                    <module>{{localnamespaceC}}_{{modulenameC}}</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </{{localnamespaceL}}_{{modulenameL}}_setup>
        </resources>

        <blocks>
            <{{localnamespaceL}}_{{modulenameL}}>
                <class>{{localnamespaceC}}_{{modulenameC}}_Block</class>
            </{{localnamespaceL}}_{{modulenameL}}>
        </blocks>

        <helpers>
            <{{localnamespaceL}}_{{modulenameL}}>
                <class>{{localnamespaceC}}_{{modulenameC}}_Helper</class>
            </{{localnamespaceL}}_{{modulenameL}}>
        </helpers>
    </global>
    <frontend>
        <routers>
            <{{localnamespaceL}}_{{modulenameL}}>
                <use>standard</use>
                <args>
                    <module>{{localnamespaceC}}_{{modulenameC}}</module>
                    <frontName>{{modulenameL}}</frontName>
                </args>
            </{{localnamespaceL}}_{{modulenameL}}>
        </routers>
        <layout>
            <updates>
                <{{localnamespaceL}}_{{modulenameL}} module="{{localnamespaceC}}_{{modulenameC}}">
                   <file>{{modulenameL}}.xml</file>
                </{{localnamespaceL}}_{{modulenameL}}>
            </updates>
        </layout>
    </frontend>
</config>

Legend:

传说:

  • localnamespaceC and localnamespaceL is the name of your chosen namespace directory in app/local/{namespace} (C is for uppercase first letter of the word, and L for lower case)

    localnamespaceC和localnamespaceL是您在app / local / {namespace}中选择的名称空间目录的名称(C表示单词的大写首字母,L表示小写)

  • modulenameC and modulenameL is the name of your module in app/local/{namespace}/{modulename}

    modulenameC和modulenameL是您在app / local / {namespace} / {modulename}中的模块名称

Magento的工厂方法 (Magento’s factory methods)

Magento makes use of factory methods to instantiate models, helpers and blocks. The methods are:

Magento利用工厂方法实例化模型,助手和块。 方法是:

  • Mage::getModel('{module}/{path to file in model directory}') – returns an instance of a class in the Model directory

    Mage::getModel('{module}/{path to file in model directory}') –返回模型目录中类的实例

  • Mage::helper('{module}/{path to file in helper directory}') – returns a singleton instance of a class in the Helper directory

    Mage::helper('{module}/{path to file in helper directory}') –返回Helper目录中类的单​​例实例

  • Mage::getSingleton('{module}/path to file in model directory') – returns a singleton instance of a class in the Model directory

    Mage::getSingleton('{module}/path to file in model directory') –返回模型目录中类的单​​例实例

  • Mage::getResourceModel('{module}/{path to file in model/resource directory}') – returns an instance of a class in the Model/Resource directory.

    Mage::getResourceModel('{module}/{path to file in model/resource directory}') –返回模型/资源目录中类的实例。

  • Mage::getBlockSingleton('{module}/{path to file in block directory}') – returns a singleton instance of a class in the Block directory after the layout for a controller action was initialized.

    Mage::getBlockSingleton('{module}/{path to file in block directory}') –初始化控制器动作的布局后,在Block目录中返回类的单例实例。

重写核心 (Rewriting the core)

Magento’s architecture offers several ways to rewrite core files. Let’s see what they are and list some pros and cons:

Magento的体系结构提供了几种重写核心文件的方法。 让我们看看它们是什么,并列出一些优点和缺点:

a) rewrite a file by creating a similar directory path and file name in the ‘local’ code pool

a)通过在“本地”代码池中创建类似的目录路径和文件名来重写文件

  • you need to copy entire class with all its properties even if one line of code needs to be changed (downside)

    您需要复制整个类及其所有属性,即使需要更改一行代码(缺点)

  • if the file is also overridden in a custom module, the tracking for applied change previously may be lost due to the fact that it doesn’t respect the standard developers are used to.

    如果该文件也在自定义模块中被覆盖,则先前跟踪的已应用更改可能会由于不遵守标准开发人员所习惯的事实而丢失。

b) rewrite the file in a Custom Module (local/MyNamespace/MyCustomModule/) and create a subdirectory with the core module name and add the original subdirectory tree (e.g. local/MyNamespace/MyCustomModule/Model/Core/Rewrite.php).

b)在自定义模块( local/MyNamespace/MyCustomModule/ )中重写文件,并使用核心模块名称创建一个子目录,并添加原始子目录树(例如,local / MyNamespace / MyCustomModule / Model / Core / Rewrite.php)。

  • to override a file in a custom module name and not using Magento’s module name for this purpose will also confuse developers and the tracking of those changes may be lost by adding another rewrite in a different place. However, by adding module configuration (in app/etc/modules/MyCustomModule.xml) to mark dependency to the Mage_Core module you can give developers a heads up that a file rewrite was made.

    在自定义模块名称中覆盖文件而不为此目的使用Magento的模块名称也将使开发人员感到困惑,并且通过在其他位置添加另一个重写可能丢失对这些更改的跟踪。 但是,通过添加模块配置(在app/etc/modules/MyCustomModule.xml )以标记对Mage_Core模块的依赖性,您可以使开发人员了解编写了文件。

c) rewrite a file in a Custom Module with Magento’s module name and preserve path (e.g. local/MyNamespace/Core/Model/Rewrite.php)

c)用Magento的模块名称在自定义模块中重写文件并保留路径(例如local/MyNamespace/Core/Model/Rewrite.php )

  • this rewrite method is the most clear as developers can keep track of changes due to the naming convention.

    这种重写方法最为清晰,因为开发人员可以根据命名约定跟踪更改。

  • will encourage other developers working on the same side to extend already changed functionality and not override it

    会鼓励其他在同一方面工作的开发人员扩展已经更改的功能,而不是覆盖它

  • a small disadvantage for this rewrite method is that the number of modules in a chosen namespace will grow along with each file that is overriden from different Magento modules.

    此重写方法的一个小缺点是,所选命名空间中的模块数量将与从不同Magento模块覆盖的每个文件一起增加。

模块之间的依赖性 (Dependency between modules)

a) when extending a Magento core module, dependency between modules needs to be configured

a)扩展Magento核心模块时,需要配置模块之间的依赖关系

e.g. app/etc/modules/MyNamespace_Customer.xml:

例如app/etc/modules/MyNamespace_Customer.xml

<MyNamespace_Customer>
    <active>true</active>
    <codePool>local</codePool>
    <priority>1</priority>
    <depends>
        <Mage_Customer/>
    </depends>
</MyNamespace_Customer>

b) when creating an sql installer or data installer in the Custom Module that updates the Magento core module entity, a dependency needs to be created

b)在更新Magento核心模块实体的自定义模块中创建sql安装程序或数据安装程序时,需要创建依赖项

e.g. installer in MyCustomModule:

例如MyCustomModule中的安装程序:

$installer = $this;
$installer->startSetup();

$sqlQuote = 'ALTER TABLE ' . $this->getTable('sales_flat_quote') .
    ' ADD `is_urgent` TINYINT UNSIGNED NOT NULL DEFAULT 0';

$installer->run($sqlOrder);
$installer->run($sqlQuote);

$installer->endSetup();

e.g. dependency config (sales quote entity is altered, dependency with ‘Sales’ module needs to be added):

例如,依赖性配置(销售报价实体已更改,需要添加具有“销售”模块的依赖性):

<MyNamespace_MyCustomModule>
    <active>true</active>
    <codePool>local</codePool>
    <priority>1</priority>
    <depends>
        <Mage_Sales/>
    </depends>
</MyNamespace_MyCustomModule>

c) Extending/Rewriting a file (model, helper, block, controller) from the Magento core module requires dependency between modules, otherwise the last changed version will be arbitrary and you will lose control over the rewrite.

c)从Magento核心模块扩展/重写文件(模型,帮助器,块,控制器)需要模块之间具有依赖性,否则最后更改的版本将是任意的,您将失去对重写的控制权。

Module dependencies are important to prioritize order of sql statements, and on a fresh install they will avoid conflicts, crashes or wrong final data.

模块依赖性对于确定sql语句的优先级很重要,并且在全新安装时,它们将避免冲突,崩溃或错误的最终数据。

结论 (Conclusion)

In this article, I’ve covered some of Magento’s fundamentals that will help developers understand the basic architecture and have a starting point in building their custom functionalities. If you’d like to see specific upgrades to Magento covered, or have use cases you’re curious about, let us know. Stay tuned for some Magento 2 tutorials and explanations as well!

在本文中,我介绍了Magento的一些基础知识,这些基础知识将帮助开发人员了解基本体系结构,并为构建自定义功能提供起点。 如果您想了解Magento的特定升级,或者对您感兴趣的用例,请告诉我们。 请继续关注一些Magento 2教程和解释!

翻译自: https://www.sitepoint.com/magento-basics-request-flow-standards-best-practices/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值