Magento2.X 后端开发简要1

Megento2.X 后端开发简要

php

根目录位置html

组件的根目录是其文件夹和文件所在的组件的顶级目录。根据您安装的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 包管理目录,不建议修改;

  

加载文件:
全部组件都须要如下文件:git

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

 

扩展生命周期:算法

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

因为主题组件和语言包一般不须要在数据库中安装数据库模式或更新数据,因此它们不须要担忧初始化或卸载任务。json

 

生命周期的类规则:后端

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

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

模式初始化是您的模块在安装、从新安装或升级时进行的第一个过程。
在架构安装期间,安装函数将在安装实现\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.

相关文章
相关标签/搜索