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.

相關文章
相關標籤/搜索