Magento2.X 后端开发简要1

Megento2.X 后端开发简要

根目录位置

组件的根目录是其文件夹和文件所在的组件的顶级目录。根据您安装的MaMeto开发环境,组件的根目录可以位于两个位置:

1.<Magento install directory>/app
    模块 For modules, use app/code.
    主题 For storefront themes, use app/design/frontend.
    后台主题 For Admin themes, use app/design/adminhtml.
    语言包 For language packages, use app/i18n.

2.<Magento install directory>/vendor    
    一般而言,这个目录为composer 包管理目录,不建议修改;

  

加载文件:
所有组件都需要以下文件:

  • registration.php   该文件还指定了在生产环境中由供应商安装组件的目录。
  • composer.json

 

扩展生命周期:

块的生命周期以及如何创建初始化或卸载时运行的可执行类。在初始化或卸载过程中,这些类可以执行数据库设置任务、升级任务、清理任务等。

由于主题组件和语言包通常不需要在数据库中安装数据库模式或更新数据,所以它们不需要担心初始化或卸载任务。

 

生命周期的类规则:

  • 类应该在您的模块的根目录中带有适当的文件名的安装目录中。
  • 类必须使用其将在其中执行的阶段的特定名称。
  • 类必须为其将在其中执行的阶段实现特定的类接口。
  • 您在模块中使用的版本应该遵循我们的版本控制策略。

模式初始化:
如果在 schema_version 表中找到模块的  setup_modules 版本,则将跳过该阶段,因为假定模块架构在先前的安装中已经初始化。

模式初始化是您的模块在安装、重新安装或升级时进行的第一个过程。
在架构安装期间,安装函数将在安装实现\Magento\Framework\Setup\InstallSchemaInterface 接口的安装架构类中执行:

// File Location: <module_root_directory>/Setup/InstallSchema.php

class \<Vendor>\<Module>\Setup\InstallSchema implements \Magento\Framework\Setup\InstallSchemaInterface
{
    /**
     * {@inheritdoc}
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        ...
    }
}

 

模式升级
如果您的模块已经安装在MaMeto中的较早版本,那么它将执行架构升级而不是安装。架构升级的目的通常是更新数据库结构或应用修补程序。

在架构升级期间,升级功能将在实现在 Magento\Framework\Setup\UpgradeSchemaInterface:

// Location: <module_root_directory>/Setup/UpgradeSchema.php

class \<Vendor>\<Module>\Setup\UpgradeSchema implements \Magento\Framework\Setup\UpgradeSchemaInterface
{
    /**
     * {@inheritdoc}
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        ...
    }
}

  

循环模式事件
您可以在模块中创建一个类,该模式将在架构安装或升级之后每次运行。在模式初始化的最后阶段运行代码的目的通常是在安装或更新数据库模式之后对其进行最终修改。

在此事件中,安装函数将在实现该类的循环类中执行。 \Magento\Framework\Setup\InstallSchemaInterface

// Location: <module_root_directory>/Setup/Recurring.php

class \<Vendor>\<Module>\Setup\Recurring implements \Magento\Framework\Setup\InstallSchemaInterface
{
    /**
     * {@inheritdoc}
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        ...
    }
}

 

数据初始化

在模块的模式被初始化之后,您的模块将通过相同的过程进行数据初始化。
就像架构安装一样,这个阶段只在您的模块的初始安装过程中运行。数据安装的目的通常是用数据库的初始数据填充数据库。
 Magento\Framework\Setup\InstallDataInterface:

// Location: <module_root_directory>/Setup/InstallData.php

class \<Vendor>\<Module>\Setup\InstallData implements \Magento\Framework\Setup\InstallDataInterface
{
    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        ...
    }
}

更新数据
就像架构升级阶段一样,数据升级只在Magento检测到以前的安装时发生。此阶段的目的通常是修复已损坏的数据或从模式更改填充新的数据字段。
 Magento\Framework\Setup\UpgradeDataInterface:

//<module_root_directory>/Setup/UpgradeData.php

class \<Vendor>\<Module>\Setup\UpgradeData implements \Magento\Framework\Setup\UpgradeDataInterface
{
    /**
     * {@inheritdoc}
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context);
    {
        ...
    }
}

 

  

循环数据事件

您可以创建一个在每个数据安装或升级之后运行的类。类的目的通常是在安装或更新数据之后对数据库存储进行最终修改。

 Magento\Framework\Setup\InstallDataInterface:

// Location: <module_root_directory>/Setup/RecurringData.php

class \<Vendor>\<Module>\Setup\RecurringData implements \Magento\Framework\Setup\InstallDataInterface
{
    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        ...
    }
}

 

  

设置资源模型
Magento 提供了  “ModuleDataSetupInterface”  and “ModuleContextInterface”  帮助数据库操作。(database manipulations)。

 

class InstallData implements InstallDataInterface
{
    /**
     * @var CustomerFactory
     */
    private $customerSetupFactory;

    /**
     * @param CustomerFactory $customerSetupFactory
     */
    public function __construct(CustomerFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var Customer $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $setup->startSetup();
        $customerSetup->installEntities();
        ...
    }
}

 

模块上下文 

若要在安装/升级类中添加更多的逻辑,可以使用 Magento 提供的 ModuleContextInterface(模块化文本界面)。上下文提供模块信息,如当前模块版本,以帮助添加逻辑到您的类。

class \Magento\Cms\Setup\InstallData implements \Magento\Framework\Setup\UpgradeDataInterface
{
   public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
   {
        if (version_compare($context->getVersion(), '1.0.0', '<')) {
            ...
        }
   }
}

  

卸载事件

 Component Manager
 卸载事命令 :

bin/magento module:uninstall --remove-data <module_name>.

在这个阶段,您的模块应该删除数据库中存在的所有踪迹;例如删除表、删除数据或还原数据。

在这个阶段,卸载函数将在卸载类中执行 Magento\Framework\Setup\UninstallInterface:

// Location: <module_root_directory>/Setup/Uninstall.php

class \<Vendor>\<Module>\Setup\Uninstall implements \Magento\Framework\Setup\UninstallInterface
{
    /**
     * {@inheritdoc}
     */
    public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        ...
    }
}

  禁用模块的卸载例程仍然可以在卸载时被调用。这意味着模块特定的配置,如依赖注入配置和事件/观察器配置将不可用,并可能导致问题。为了避免这种情况,卸载类不应该依赖它们。

  

 

路由:

在 Magento 系统中,URL具有以下格式:

<area front name>/<VendorName>/<ModuleName>/<controller name>/<action name>

<area front name> 表示它位于URL的“前面”。(区域名称在内部用于指配置文件中的区域。Magento 为管理区域提供诸如店面 frontend 和adminhtml等区域。

若要将URL分配给相应的控制器和操作,请使用路由器类。

路由器有一个算法来寻找匹配的控制器,由请求确定。

然后,根据路由规则,将控制器分配给URL。使用routes.xml文件来检查或更改路由规则。

路由 

模块的路由器信息在路由器列表参数中描述 Magento\Framework\App\RouterList 输入你的 di.xml.

每个区域都有自己的路由器集合。将 Magento 框架 Magento\Framework\App\RouterList 模型注入前端控制器。

您可能需要定制路由器来改变处理请求的标准逻辑或本地 Magento 路由器(如CMS路由器、默认路由器等)。但是,您不能定制在Magento核心模块中使用的路由器。

 

路由的配置存储在 routes.xml。

只有标准前端和后端路由器使用路由。通常,路由的配置具有以下格式:

<config>
    <router id="%routerId%">
        <route id="%routeId%" frontName="%frontName%">
            <module name="%moduleName%" before="%moduleName%"/>
        </route>
    </router>
</config>

  

%routeId%的长度必须至少为三个字符,并且可以由以下字符组成:A-Z, a-z, 0-9, _.
%frontName%长度必须至少为三个字符,并且可以由以下字符组成:A-Z, a-z, 0-9, _, -.

  

若要检索指定路由器对区域的路由配置,请使用 Magento\App\Framework\Route\Config.

若要在自定义的路由中替换控制器动作,请在原始控制器之前添加自定义控制器类。

自定义控制器和操作应该与原始的共享相同的名称。

该系统在原始控制器之前处理自定义控制器,而路由保持相同。

如果必须重置路由和设计,则将请求处理转发到另一路由:

$this->_forward('other/controller/action')

为了移除控制器动作,例如,转发到instance, in app/code/Company/SomeExtension/Controller/Account/Create.php:;

namespace Company\SomeExtension\Controller\Account;

class Create extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        $this->_forward('noroute');
    }
}

  

路由处理

 

路由的处理方式如下:

  • 模块通过路由器的 routerListparameter 提供关于路由器的信息。Magento\Framework\App\RouterList type in di.xml.
  • FrontController 获得并检查是否可以处理请求。
  • 如果请求不能被任何路由器处理,则使用默认路由器。
  • 如果请求可以由路由器处理,则路由器找到具有匹配的FrnToN名字的路由并查看相应的模块。如果模块具有匹配的控制器和动作名称,则路由器实例化该控制器。

The dispatch() method of the Magento\Framework\App\Action\Action class requests an instance and returns its response.
dispatch():请求实例并返回其响应。

For this class, the Magento\Framework\App\ActionInterface processes the requests through its actions. Also, the following classes participate in processing the requests:
Magento\Framework\App\ActionInterface : 通过其动作处理请求。此外,以下类参与处理请求:

  • The Magento\Framework\App\State class provides information on the state of the application, that is, current mode, installation date, and so on.(提供有关应用程序状态的信息,即当前模式、安装日期等。)
  • The Magento\Framework\App\Arealist class serves to configure the application areas through the di.xml file
    (类用于通过di.xml文件配置应用程序区域。)
  • The Magento\App\Area\FrontNameResolverInterface class resolves the dynamic area’s front names (类解析动态区域的前缀名。)

    

 

Default router

 

If a request cannot be processed by any router, the Magento\App\Framework\Router\DefaultRouter default router lists handlers for processing such request.
如果请求不能被任何路由器处理, Magento\App\Framework\Router\DefaultRouter 路由器默认列出处理这种请求的处理程序。

 

Magento\App\Router\NoRouteHandlerList 包含处理程序列表。.

 

 

See The Route Config Kata by Magento contributor Vinai Kopp.
查看路由配置请到Magento的作者:Vinai Kopp.

 

posted @ 2018-06-27 09:48  徐锅  阅读(517)  评论(0编辑  收藏  举报