DEV Community

Cover image for A Scalable and Maintainable Approach for Laravel Applications
Aung Kyaw Minn
Aung Kyaw Minn

Posted on • Edited on

A Scalable and Maintainable Approach for Laravel Applications

Introducing Mojura Architecture

In the ever-evolving world of web development, maintaining a scalable and readable codebase is crucial. Enter Mojura Architecture, a set of principles designed to enhance your development workflow by promoting scalability, maintainability, and readability. Inspired by the Japanese word "モジュラ" (module), Mojura Architecture breaks down your application into manageable units, making it easier to scale, maintain, and understand.

Key Benefits

Scalability

By breaking down your application into smaller, manageable units, Mojura Architecture allows you to easily scale your project as it grows. This modular approach ensures that each part of your application can be developed and scaled independently.

Maintainability

With a clear separation of concerns, Mojura Architecture makes it easier to maintain and update your codebase. This reduces the risk of bugs and improves overall code quality, making your application more robust and reliable.

Readability

The structured approach of Mojura Architecture ensures that your code is organized and easy to understand. This makes it simpler for new developers to get up to speed and contributes to a more efficient development process.

Concept

  1. Route
    • Call Controller functions
  2. Controller
    • Serve the Features
    • Return Response received from Feature to the Request
  3. Feature
    • Validate the Request
    • Run the Job by passing the request parameters
    • Collect Return Data from Job
    • Prepare Response Data
    • Return the HTTP Response to the Controller Method
  4. Request
    • Authorize the Request (Optional)
    • Implement HTTP Request validating
  5. Job
    • Do the actual work by implementing the business logic

Principles

  1. Feature Serves a Single Purpose: Keep features simple and focused.
  2. Job Executes a Single Responsibility: Ensure each job handles one responsibility, even if it involves multiple related functions.
  3. Modules Shouldn’t Cross: Each module should be self-contained and should not perform tasks that belong to other modules.
  4. Apply Decoupling Techniques: Use shared helper classes to enhance code reusability and maintainability.
  5. Features Shall Not Call Other Features: Maintain feature independence.
  6. Jobs Shall Not Call Other Jobs: Keep business logic concise and organized.
  7. Write Code That Humans Can Read: Prioritize readability for easier maintenance and collaboration.

Naming Conventions

Consistent naming for files and classes is encouraged for clarity and readability. Here are some examples:

  • Module: [Subject]Module (e.g., UserModule)
  • Controller: [Subject]Controller (e.g., UserController)
  • Feature: [Operation][Subject]Feature (e.g., CreateUserFeature)
  • Job: [Operation][Subject]Job (e.g., CreateUserJob)
  • Request: [Operation][Subject]Request (e.g., CreateUserRequest)

Directory Structure

Mojura Architecture promotes a clear and organized directory structure:

laravel-project/
├── app/
│   ├── Modules/
│   │   ├── YourModule1/
│   │   │   ├── Features/
│   │   │   ├── Http/
│   │   │   │   ├── Controllers/
│   │   │   │   └── Requests/
│   │   │   └── Jobs/
│   │   ├── YourModule2/
│   │   │   ├── Features/
│   │   │   ├── Http/
│   │   │   │   ├── Controllers/
│   │   │   │   └── Requests/
│   │   │   └── Jobs/
│   │   ├── YourModuleN/
│   │   │   ├── Features/
│   │   │   ├── Http/
│   │   │   │   ├── Controllers/
│   │   │   │   └── Requests/
│   │   │   └── Jobs/
├── routes/
│   ├── api/
│   └── web/
└── .env
Enter fullscreen mode Exit fullscreen mode

Mojura Laravel Package

A lightweight Laravel Package that implements the Mojura Architecture principles.

Installation

To install the Mojura package into your Laravel 10+ application using Composer:

composer require innoaya/mojura
Enter fullscreen mode Exit fullscreen mode

Configuration

Publish the configuration file:

php artisan vendor:publish --tag=mojura-config
Enter fullscreen mode Exit fullscreen mode

Generating Components

  • Route: php artisan mojura:route [RouteFileName] [VersionDirectory] [--web] [--force]
  • Controller: php artisan mojura:controller [Controller] [Module] [Directory] [--force]
  • Request: php artisan mojura:request [Request] [Module] [Directory] [--force]
  • Feature: php artisan mojura:feature [Feature] [Module] [Directory] [--force]
  • Job: php artisan mojura:job [Job] [Module] [Directory] [--force]

Documentation

https://mojura.innoaya.org

Source Code

https://github.com/innoaya/mojura

Starter Kit

This starter kit provides a fully implemented backend solution based on the Mojura Architecture concepts, designed to speed up your development process. It includes robust features for authentication, authorization, user management, and security, making it a comprehensive starting point for building scalable applications.
https://github.com/innoaya/mojura-laravel-starter

Top comments (0)