Espace de noms: Mastering \ Itdesire
1.Comment créer un module
Mastering / Itdesire / etc / module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mastering_Itdesire" setup_version="1.0.1">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
Mastering / Itdesire / registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Mastering_Itdesire',
__DIR__
);
2.Comment installer et mettre à niveau la base de données:
Installer le schéma:
Mastering / Itdesire / Setup / InstallSchema.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of InstallSchema
*
* @author pramod
*/
namespace Mastering\Itdesire\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;
class InstallSchema implements InstallSchemaInterface
{
/**
* {@inheritdoc}
*/
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$table = $setup->getConnection()->newTable(
$setup->getTable('mastering_itdesire_item')
)->addColumn(
'id',
Table::TYPE_INTEGER,
null,
['identity' => true, 'nullable' => false, 'primary' => true],
'Item ID'
)->addColumn(
'name',
Table::TYPE_TEXT,
255,
['nullable' => false],
'Item Name'
)->addIndex(
$setup->getIdxName('mastering_itdesire_item', ['name']),
['name']
)->setComment(
'itdesire Items'
);
$setup->getConnection()->createTable($table);
$setup->endSetup();
}
}
Installer les données:
Mastering / Itdesire / Setup / InstallData.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Mastering\Itdesire\Setup;
/**
* Description of InstallData
*
* @author pramod
*/
namespace Mastering\Itdesire\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
/**
* {@inheritdoc}
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$setup->getConnection()->insert(
$setup->getTable('mastering_itdesire_item'),
[
'name' => 'Item 1'
]
);
$setup->getConnection()->insert(
$setup->getTable('mastering_itdesire_item'),
[
'name' => 'Item 2'
]
);
$setup->endSetup();
}
}
Schéma de mise à niveau:
Mastering / Itdesire / Setup / UpgradeSchema.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Mastering\Itdesire\Setup;
/**
* Description of UpgradeSchema
*
* @author pramod
*/
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* {@inheritdoc}
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
if (version_compare($context->getVersion(), '1.0.1', '<')) {
$setup->getConnection()->addColumn(
$setup->getTable('mastering_itdesire_item'),
'description',
[
'type' => Table::TYPE_TEXT,
'nullable' => true,
'comment' => 'Item Description'
]
);
}
$setup->endSetup();
}
}
Mettre à niveau les données:
Mastering / Itdesire / Setup / UpgradeData.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Mastering\Itdesire\Setup;
/**
* Description of UpgradeData
*
* @author pramod
*/
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
class UpgradeData implements UpgradeDataInterface
{
/**
* {@inheritdoc}
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
if (version_compare($context->getVersion(), '1.0.1', '<')) {
$setup->getConnection()->update(
$setup->getTable('mastering_itdesire_item'),
[
'description' => 'Default description'
],
$setup->getConnection()->quoteInto('id = ?', 1)
);
}
$setup->endSetup();
}
}
3.Comment créer des couches de modèle, de vue et de contrôleur
Manette :
Mastering / Itdesire / Controller / Adminhtml / Index / Index.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Index
*
* @author pramod
*/
namespace Mastering\Itdesire\Controller\Adminhtml\Index;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action;
class Index extends Action{
//put your code here
public function execute() {
/** @var \Magento\Framework\Controller\Result\Raw $result */
$result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
$result->setContents('Hello Admins!');
return $result;
}
}
Mastering / Itdesire / Controller / Index / Index.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Index
*
* @author pramod
*/
namespace Mastering\Itdesire\Controller\Index;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Action;
class Index extends Action{
//put your code here
public function execute() {
/** @var \Magento\Framework\Controller\Result\Raw $result */
/*
$result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
$result->setContents('Hello World!');
return $result; */
return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
}
}
Mastering / Itdesire / etc / adminhtml / routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="mastering" frontName="mastering">
<module name="Mastering_Itdesire"/>
</route>
</router>
</config>
Mastering / Itdesire / view / frontend / layout / mastering_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block name="mastering_hello" class="Mastering\Itdesire\Block\Hello" template="hello.phtml"/>
</referenceContainer>
</body>
</page>
Modèle, modèle de ressource et API:
Mastering / Itdesire / Model / ResourceModel / Item / Collection.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Collection
*
* @author pramod
*/
namespace Mastering\Itdesire\Model\ResourceModel\Item;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Mastering\Itdesire\Model\Item;
use Mastering\Itdesire\Model\ResourceModel\Item as ItemResource;
class Collection extends AbstractCollection {
protected $_idFieldName = 'id';
protected function _construct()
{
$this->_init(Item::class, ItemResource::class);
}
}
Mastering / Itdesire / Model / ResourceModel / Item.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Item
*
* @author pramod
*/
namespace Mastering\Itdesire\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Item extends AbstractDb {
protected function _construct()
{
$this->_init('mastering_itdesire_item', 'id');
}
}
Mastering / Itdesire / Model / Config.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Mastering\Itdesire\Model;
use Magento\Framework\App\Config\ScopeConfigInterface;
/**
* Description of Config
*
* @author pramod
*/
class Config {
const XML_PATH_ENABLED = 'mastering/general/enabled';
private $config;
public function __construct(ScopeConfigInterface $config)
{
$this->config = $config;
}
public function isEnabled()
{
return $this->config->getValue(self::XML_PATH_ENABLED);
}
}
Mastering / Itdesire / Model / Item.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Item
*
* @author pramod
*/
namespace Mastering\Itdesire\Model;
use Magento\Framework\Model\AbstractModel;
class Item extends AbstractModel{
//put your code here
protected function _construct() {
$this->_init(\Mastering\Itdesire\Model\ResourceModel\Item::class);
}
}
Mastering / Itdesire / Model / ItemRepository.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Mastering\Itdesire\Model;
/**
* Description of ItemRepository
*
* @author pramod
*/
use Mastering\Itdesire\Api\ItemRepositoryInterface;
use Mastering\Itdesire\Model\ResourceModel\Item\CollectionFactory;
class ItemRepository implements ItemRepositoryInterface {
private $collectionFactory;
public function __construct(CollectionFactory $collectionFactory) {
$this->collectionFactory = $collectionFactory;
}
public function getList() {
return $this->collectionFactory->create()->getItems();
}
}
Vue :
Mastering / Itdesire / Block / Hello.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Hello
*
* @author pramod
*/
namespace Mastering\Itdesire\Block;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Mastering\Itdesire\Model\ResourceModel\Item\Collection;
use Mastering\Itdesire\Model\ResourceModel\Item\CollectionFactory;
class Hello extends Template{
//put your code here
private $collectionFactory;
public function __construct(Context $context,
CollectionFactory $collectionFactory,
array $data = array()) {
$this->collectionFactory = $collectionFactory;
parent::__construct($context, $data);
}
/**
* @return \Mastering\Itdesire\Model\Item[]
*/
public function getItems()
{
return $this->collectionFactory->create()->getItems();
}
}
Mastering / Itdesire / view / frontend / templates / hello.phtml
<?php
/***
* @var \Mastering\Itdesire\Block\Hello $block
* ***/
?>
<?php foreach ($block->getItems() as $item){
?>
<p>
<?php echo $item->getName();?>: <?php echo $item->getDescription();?>
</p>
<?php } ?>
4.Comment créer la commande CLI Magento:
Mastering / Itdesire / Console / Command / AddItem.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of AddItem
*
* @author pramod
*/
namespace Mastering\Itdesire\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Mastering\Itdesire\Model\ItemFactory;
use Magento\Framework\Console\Cli;
class AddItem extends Command{
const INPUT_KEY_NAME ="name";
const INPUT_KEY_DESCRIPTION ="description";
private $itemFactory;
public function __construct(ItemFactory $itemFactory) {
$this->itemFactory = $itemFactory;
parent::__construct();
}
protected function configure() {
$this->setName("itdesire:item:add")
->addArgument(
self::INPUT_KEY_NAME,
InputArgument::REQUIRED,
'Input Name'
)->addArgument(
self::INPUT_KEY_DESCRIPTION,
InputArgument::OPTIONAL,
"Input Description"
);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output) {
$item = $this->itemFactory->create();
$item->setName($input->getArgument(self::INPUT_KEY_NAME));
$item->setDescription($input->getArgument(self::INPUT_KEY_DESCRIPTION));
$item->setIsObjectNew(true);
$item->save();
return Cli::RETURN_SUCCESS;
}
}
5.Comment configurer la tâche planifiée dans Magento 2:
Mastering / Itdesire / Cron / AddItem.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of addItem
*
* @author pramod
*/
namespace Mastering\Itdesire\Cron;
use Mastering\Itdesire\Model\ItemFactory;
use Mastering\Itdesire\Model\Config;
class AddItem {
private $itemFactory;
private $config;
public function __construct(ItemFactory $itemFactory,Config $config) {
$this->itemFactory = $itemFactory;
$this->config = $config;
}
public function execute()
{
if($this->config->isEnabled()){
$this->itemFactory->create()
->setName('Scheduled item')
->setDescription('Created at ' . time())
->save();
}
}
}
Maîtriser / Itdesire / etc / config.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<mastering>
<general>
<enabled>0</enabled>
<cron_expression>* * * * *</cron_expression>
</general>
</mastering>
</default>
</config>
Mastering / Itdesire / etc / crontab.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job name="masteringAddItem" instance="Mastering\Itdesire\Cron\AddItem" method="execute">
<config_path>mastering/general/cron_expression</config_path>
</job>
</group>
</config>
Mastering / Itdesire / etc / adminhtml / system.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="mastering" translate="label" sortOrder="10000">
<label>Mastering</label>
</tab>
<section id="mastering" translate="label" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Mastering</label>
<tab>mastering</tab>
<resource>Mastering_Itdesire::mastering</resource>
<group id="general" translate="label" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Item creation by schedule</label>
<field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="cron_expression" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
<label>Add Item Cron Expression</label>
</field>
</group>
</section>
</system>
</config>
6.Comment créer une entrée de menu:
Mastering / Itdesire / etc / adminhtml / menu.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
<menu>
<add id="Mastering_Itdesire::mastering" title="Mastering"
module="Mastering_Itdesire" resource="Mastering_Itdesire::mastering"
translate="title" sortOrder="900" action="mastering"/>
</menu>
</config>
7.Comment créer une ressource ACL:
Masterisation / Itdesire / etc / acl.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Masteting_Itdesire::mastering" title="Mastering"
translate="title" sortOrder="900"/>
</resource>
</resources>
</acl>
</config>
8.Comment implémenter une API Web dans Magento avec une table personnalisée:
Mastering / Itdesire / etc / di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="masteringAddItem" xsi:type="object">Mastering\Itdesire\Console\Command\AddItem</item>
</argument>
</arguments>
</type>
<preference type="Mastering\Itdesire\Model\Item" for="Mastering\Itdesire\Api\Data\ItemInterface"/>
<preference type="Mastering\Itdesire\Model\ItemRepository" for="Mastering\Itdesire\Api\ItemRepositoryInterface"/>
</config>
Mastering / Itdesire / etc / webapi.xml
<?xml version="1.0"?>
<routes>
<route url="/V1/mastering" method="GET">
<service class="Mastering\Itdesire\Api\ItemRepositoryInterface" method="getList"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>