Giter VIP home page Giter VIP logo

laravel-modular's Introduction

Laravel Modular App Plugin

Laravel Release Issues Licence

Turn your existing laravel app into modular application. Laravel modular plugin allows you to write modular plugins for laravel.

Let's say, you are building a blog application. Your blog needs to have following features:

  • comments
  • blog post
  • user management etc..

You can turn this features into a module and bundle your logic so that you can easily use this module for your other projects. You can easily turn on/off your module.

Plugin Requirements

  • PHP >= 7.2
  • Laravel >= 6.0

Newer Laravel Plugin

For older version >= 8.0 of Laravel use this plugin Modular Laravel

How to install this plugin

Run following command to your existing project.


# install this plugin
composer require "learn2torials/laravel-modular"

# create module using artisan
php artisan make:module comments

Above command will create a new directory under App/Modules with following structure.

App
|- Modules
   |-- Comments
      |-- Controllers
      |-- Models
      |-- Views
      |-- Migrations
         |-- Seeder
      |-- Translations
         |-- en
             |-- general.php
         |-- fr
             |-- general.php
      |-- config.php
      |-- routes.php

Next, once this folder structure is generated you can turn on this module by creating console.php file in config directory.

<?php

/*
|--------------------------------------------------------------------------
| Configuration File
|--------------------------------------------------------------------------
|
| You can overwrite default configuration here according to your app requirements.
|
*/
return [
    "prefix"   => null,
    "i18n"     => false,
    "https"    => false,
    "modules"  => [
        "comments" => true
    ]
];

That is it, your module is now enabled. You can verify that your module is working by browsing

http://yourdomain.com/comments

Add prefix before all your modules. Set following config in config/console.php file.

"prefix" => "admin",

Now, your module url will be:

http://yourdomain.com/admin/comments

Enable translation for you module. Set following config in config/console.php file.

"i18n" => true,

Now, your module url will be:

http://yourdomain.com/en/ca/comments      -> for english translation
http://yourdomain.com/fr/ca/comments      -> for french translation

When prefix is enabled

http://yourdomain.com/en/ca/admin/comments -> if prefix is admin
http://yourdomain.com/fr/ca/admin/comments -> if prefix is admin

How to use translations. Check your view file in your module to get the idea of usage:

{{ __('module::file_name.translation_key') }}

Module Configurations

Once module is enabled you can access module related configurations using following syntax.

For example: if you have installed a user module. Configuration file for user module is located in Modules/User/config.php

<?php

/*
|--------------------------------------------------------------------------
| User Module Configurations
|--------------------------------------------------------------------------
|
| Here you can add configurations that relates to your [User] module
| Only make sure not to add any other configs that do not relate to this
| User Module ...
|
*/
return [

    // module name
    'name' => 'User',

    // register middleware
    'middleware' => [
        'user' => \App\Modules\User\Middleware\UserAuthenticated::class,
    ],

    // register service providers
    'providers' => [
        \App\Modules\User\Provider\UserProvider::class
    ],

    // register route middleware
    'route_middleware' => ['user'],

    // database seeder
    'seeder' => [
        __DIR__. '/Migrations/Seeder/UsersTableSeeder.php'
    ]
];

How to run module migration/seeder

To run migration or seeder for your modules. Add seeder to config file and run following commands.


# run module migrations
php artisan migrate

# run module seeders
php artisan db:seed --class="L2T\Database\Seeder"

Reference

Example is shown on https://learn2torials.com/a/laravel-module-management

laravel-modular's People

Contributors

learn2torials avatar phpcodebooster avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.