How to Work with Magento Extensions/ Magento 2 Database

How to Work with Magento Extensions/ Magento 2 Database

30 min read

Send to you:

Magento extensions are the combination of functions that can be added to any Magento store to enhance its functionality. If you are looking for the general setup and the initial steps on how to create Magento extension – this section is formed for your help. Also, we’ve covered the question of how to work with databases and set up the cron job.

Below you can find frequently asked questions and solutions for each of them.

What is Magento 2 Marketplace?

Magento Marketplace, the official Magento extension store, is the global eCommerce resource for applications and services that expand Magento solutions with powerful new features and functionality. Now global retailers can do even more with their digital stores.

This source is designed for easy discovery of relevant Magento extensions. It provides a curated user experience with offerings from top brands and new innovators. Discover new and customized functionality, in key business categories, to help your business thrive.

Customers can trust that all extensions and providers with offerings in Magento Marketplace are reviewed and checked for quality. In addition, Magento Technology Partners participate in a manual code and documentation review…for an even higher level of quality assurance.

So, how to install, create and uninstall any extension from your Magento store, you can find below.

How to Install Extension for Magento 2 in Localhost?

Step 1: Download/Purchase the extension.

Step 2: Unzip the file in a temporary directory.

For example, on Linux you can use next command:

unzip exten_file.zip -d somedir

Step 3: Upload it to your Magento installation root directory.

Step 4: Disable the cache.

Go to System­ >> Cache Management or use the command:

sudo bin/magento cache:flush

Step 5: Run the command.

Enter the following at the command line:

sudo bin/magento setup:upgrade

Step 6: Find your extension installed.

After opening Stores­ >>Configuration >­>Advanced >­> Admin, the module will be shown in the admin panel or, you can find your extension on list modules that are displayed in command line after step 5.

How to Create Custom Extension in Magento?

Step 1. Disable Magento cache (recommended).

Disabling Magento cache during development will save you some time because you won’t need to manually flush the cache every time you make changes to your code.

The easiest way to disable cache is to go to Admin >> System >> Cache Management >> select all cache types and disable them.

Step 2. Put Magento into a developer mode (recommended).

You should put Magento into a developer mode to ensure that you see all the errors Magento is throwing at you.

In order to do this, open your terminal and go to the Magento 2 root. From there you should run the following command:

php bin/magento deploy:mode:set developer

Step 3. Create the folder of Hello World module.

Name of the module is defined as “VendorName_ModuleName”. The first part is the name of the vendor and the last part is the name of the module: For example Magento_HelloWorld.

Focus on the following guide to create the folders:

app/code/NS/Firstmodule

Step 4. Create etc/module.xml file.

Then, it is necessary to create an etc folder and add the module.xml file:

app/code/NS/Firstmodule/etc/module.xml

Contents would be:

<?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="NS_Firstmodule" setup_version="0.0.1"/>
</config>

Step 5. Create etc/registration.php file

In this step, we will add registration.php as the following guide:

app/code/NS/Firstmodule/registration.php

Contents would be:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
       \Magento\Framework\Component\ComponentRegistrar::MODULE,
       'NS_Firstmodule',
       __DIR__
   );

Step 6. Enable the module.

Finish the step 5, we have already created Firstmodule. And we will enable this module in this step.

After creating the module run the next commands:

php bin/magento setup:upgrade

If all is good, you will see your module in the list, for example:

Custom Module in the Module List

php bin/magento setup:static-content:deploy

Step 7. Creating a controller.

          7.1. First, we need to define the router.

To do this, create a routes.xml file in the

app/code/NS/Firstmodule/etc/frontend/

folder with the following code:

<?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="standard">
       <route id="firstmodule" frontName="firstmodule">
           <module name="NS_Firstmodule" />
       </route>
   </router>
</config>

After defining the first part of the route, the URL will be displayed as:

http://<yourhost.com>/firstmodule/*

         7.2. Create a controller

The folder and file you need to create are:

app/code/NS/Firstmodule/Controller/Index/HelloWorld.php

Contents would be:

<?php
namespace NS\Firstmodule\Controller\Index;
class HelloWorld extends \Magento\Framework\App\Action\Action{
   public function execute()
   {
       echo "Hello! It’s my first module of Magento 2!";
       exit;
   }

After completed, please run sudo bin/magento cache:flush
to check result.

Your URL now should be as:

http://<yourhost.com>/firstmodule/index/helloWorld

After finish all steps, the output Hello! It’s my first module of Magento 2! should be displayed in your browser when you open the URL.

How Module Looks from the Browser

How to Uninstall Magento 2 Extension?

If the extension is installed via composer, then you can uninstall Magento 2 extension this way:

Step 1. Remove extension from the composer.json file
Step 2. Execute command composer update
Step 3. Execute command bin/magento setup:upgrade
Step 4. Execute command bin/magento setup:di:compile
Step 5. Execute command bin/magento setup:static-content:deploy

If the extension is installed to app/code directly, it will be uninstalled this way:

Step 6. Remove extension from app/code. It’s enough to remove extension registration.php file
Step 7. Execute command bin/magento setup:upgrade
Step 8. Execute command bin/magento setup:di:compile
Step 9. Execute command bin/magento setup:static-content:deploy

Next, we move on to work with a database and set up the cron job.

How to Find Database Name in Magento 2

Go to your store core folder and open the env.php file under the app/etc folder.

Find the next code, where database_name is the actual database name which you use for your Magento 2 store.

array (
 'host' => 'host_name,
 'dbname' => 'database_name',
 'username' => 'your username',
 'password' => 'your password',
),

How to Change Database Name in Magento?

In Magento 1.0, the configuration file that contains database settings and other information is located here:/app/etc/local.xml

In Magento 2.0, the configuration is in the same directory (/app/etc/) but has been renamed to env.php. The file path is: /<Magento Install Dir>/app/etc/env.php

Navigate to your Magento 2 installation directory and edit app/etc/env.php configuration file in your favorite editor

For example :

'db' =>
array (
 'table_prefix' => '',
 'connection' =>
 array (
   'default' =>
   array (
     'host' => 'localhost',
     'dbname' => 'Magento2',
     'username' => 'root',
     'password' => 'root',
     'active' => '1',
   ),
 ),
),
 

Where:
host – is your MySQL server hostname
dbname  MySQL database name of Magento
username – MySQL user to connect database server
password – MySQL user password

After changes in this file you need to run the command: sudo bin/magento setup:di:compile

That’s how you change Magento database name.

How to Setup Cron Job in Magento 2?

Magento 2 Cron job may be set up by module file etc/crontab.xml, that will have such content.

<?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="download_expire_link" instance="Web4pro\Download\Model\Cron" method="execute">
           <schedule>0 1 * * *</schedule>
       </job>
   </group>
</config>

Class Web4pro\Download\Model\Cron should exist and implement method execute, that will run when cron job starts.

4.9/5.0

Article rating (9 Reviews)

Do you find this article useful? Please, let us know your opinion and rate the post!

  • Not bad
  • Good
  • Very Good
  • Great
  • Awesome