CommercePeer Brand Logo


Programmatically: Magento 2 Product Entity

Programmatically: How to create custom Magento extensions

Step 1: Create a new directory for your extension

Create a new directory named MyExtension in the app/code directory of your ="Magento" installation.

Step 2: Create a config.xml file

In the MyExtension directory, create a new file named config.xml. This file will define our extension's configuration.


	<?xml version="1.0"?>
	<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/moduleconfig.xsd">
    <module name="MyCompany_MyModule" setup_version="1.0.0">
        <depends>
            <module>Magento_Catalog</module>
        </depends>
    </module>
	</config>
	

Step 3: Create a module.xml file

In the same MyExtension directory, create a new file named module.xml. This file will define the modules that our extension depends on.

	  <?xml version="1.0"?>
	  <config>
		<modules>
		  <MyExtension>
			<depends>Magento_Core</depends>
		  </MyExtension>
		</modules>
	  </config>
	

Step 4: Create a etc/module.xml file

In the same MyExtension directory, create a new file named etc/module.xml. This file will define our extension's module declaration.

	<?xml version="1.0"?>
	<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Object/etc/module.xsd">
    <module name="MyExtension" setup_version="1.0.0">
        <dependencies>
            <module name="Magento_Core" />
        </dependencies>
    </module>
	</config>
	

Step 5: Create a controller

In the MyExtension directory, create a new directory named controllers. Inside this directory, create a new file named IndexController.php.


namespace MyExtension\Controller\Index;

class IndexController extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        // Your code here
        echo "Hello World!";
        exit;
    }
}
	

Step 6: Define the route

In the same controllers directory, create a new file named index.php.

	<?xml version="1.0"?>
	<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/config.xsd">
		<routes>
			<my_module_index_index translate="label">
				<label>My Module Index</label>
				<controller>my_module/index</controller>
				<action>index</action>
			</my_module_index_index>
		</routes>
	</config>
	

LET’S WORK TOGETHER

We love working with Small Businesses, Retailers, Manufacturers. Our team is here to Help.

→ Schedule Free Consultant Now
Copyright © 2024 CommercePeer. All Rights Reserved.