Mage2 Laravel E commerce Module Service Provider

Modules are the primary way of adding functionality to Mage2 E commerce App.

These modules may have routes, controllers, views, admin menus, permission, and configuration specifically intended to enhance a Mage2 E commerce app. This guide primarily covers the development of those modules that are Mage2 E commerce specific.

# Directory Structure

Modules Directory Structure for Laravel E commerce

 

As you can see Module.php file is the Entry point for your modules. Which contains the code for registering your Route, views, migration, configs and etc. It does have two main method boot and register

# Routes

To register your modules's routes with Mage2 E commerce you need to tell application where the routes are located. You may load the routes using the loadRoutesFrom method. The loadViewsFrom method accepts one arguments: the path to your routes file. This method will automatically determine if the application's routes are cached and will not load your routes file if the routes have already been cached:

/**
 * Perform post-registration booting of services.
 *
 * @return  void
 */
public function boot()
{
    $this->loadRoutesFrom(__DIR__.'/routes/web.php');
}

# Views

To register your modules's views with Mage2 E commerce you need to tell application where the views are located. You may load the views using the loadViewsFrom method. The loadViewsFrom method accepts one arguments: the path to your view templates and your modules's namespace. For example, if your module's namespace is mage2-hello-world, you would add the following to your Module.php file boot method:

/**
 * Perform post-registration booting of services.
 *
 * @return  void
 */
public function boot()
{
     $this->loadViewsFrom(__DIR__.'/views', 'mage2-hello-world');
}

# Migration

To register your modules's migration with Mage2 E commerce you need to tell application where the migration files are located. You may load the migration using the loadMigrationsFrom method. The loadMigrationsFrom method accepts two arguments: the path to your migration files.

/**
 * Perform post-registration booting of services.
 *
 * @return  void
 */
public function boot()
{
     $this->loadMigrationsFrom(__DIR__.'/database');
}

# Admin Menu

To register your modules's admin menu with Mage2 E commerce you need to use the register method. You may use the AdminFacade to register it.

use Mage2\Framework\AdminMenu\Facades\AdminMenu;
/*
 * Register bindings in the container.
 *
 * @return  void
 */
public function register()
{
     // This menu will display under catalog dropdwon
     $adminUserMenu = ['catalog' => [
            'submenu' => [
                'hello-world' => [
                    'label' => 'Admin Hello World',
                    'route' => 'admin.hello-world.index',
                ]
            ]
        ]];
        AdminMenu::registerMenu('mage2-catalog', $adminUserMenu);
}