Writing modular applications with laravel-modules
01-07-2016
Let me start by saying Laravel is an amazing framework. However when it comes to writing more complex and bigger applications, I find the default structure laravel comes with cumbersome and not ideal.
The way the default laravel installation comes with is basically all the application logic inside an app/ folder. This works, but I would not suggest going this route.
Just imagine having a medium sized applicated where everything is in the app/ folder, worse, every model is in the root of the app folder! At some point you will spend a lot of time looking for things because everything is bunched together.
Enter the modular world
This is what being modular is trying to resolve. You split of the business logic into different parts, which belongs together. If you're into Domain Driven Design, you can consider a module an aggregate.
Every module has its own routes/controllers/models/views/business logic/etc. Meaning every module contains a group of classes that all are related to each other in some way.
Example
Consider an application where you'd have products and a shopping cart. To keep it simple.
You would have something like this:
app/
- Http
- Controllers
- Admin
- ProductsController
- ProductCategoryController
- OrderController
- OrderStatusController
- Frontend
- ProductsController
- CartController
- CartAddressController
- CartPaymentController
- CartReviewController
- CartSuccessController
- ...
- Requests
- CreateProductRequest
- UpdateProductRequest
- CreateProductCategoryRequest
- UpdateProductCategoryRequest
- CreateAddressRequest
- UpdateAddressRequest
- ...
- Middleware
- HasCartItem
- routes.php
- Models
- Product
- ProductCategory
- Cart
- CartItem
- User
- UserAddress
- Order
As you can see I'm leaving a lot of classes out or this would be a lot bigger. We're not even covering repositories, views, service classes and more. Even this few classes already show that application is becoming a mess. Applications also have more than just products and carts, so the mess would be even worse.
Now lets see what this could look like with a modular approach.
Modules/
- Cart
- Http
- Controllers
- Frontend
- CartController
- CartAddressController
- CartPaymentController
- CartReviewController
- CartSuccessController
- Requests
- CreateAddressRequest
- UpdateAddressRequest
- Middleware
- HasCartItem
- adminRoutes.php
- frontendRoutes.php
- Models
- Cart
- CartItem
- Repositories
- resources
- lang
- views
- Product
- Http
- Controllers
- Admin
- ProductController
- ProductCategoryController
- Frontend
- ProductController
- adminRoutes.php
- frontendRoutes.php
- Requests
- CreateProductRequest
- UpdateProductRequest
- CreateProductCategoryRequest
- UpdateProductCategoryRequest
- Models
- Product
- ProductCategory
- Repositories
- resources
- lang
- views
With this structure, everything that belongs together is grouped into one namespace. This also means that you don't end up with one huge routes file for instance.
When you need to find something, you directly know where to search, in which folder you can dig through.
Granted, there are more folders, but it has the advantage of being clear at a birds eye view. Uncle Bob has a good video about architecture on why keeping everything in app/ isn't a good idea.
Modules in Laravel
Now you must be thinking how do I implement this in laravel ? At its basics it's fairly easy, you can just autoload the Modules folder using PSR-4 and be done with it.
However that leaves more work to you to register the custom view/lang/config namespaces, being able to have migrations in each module and run migrations of an individual module. Having frontend assets per module, and having a quick way to publish them to the public/ directory. Also an easy way to access to correct asset based on a given module name. etc. etc.
TL;DR, there is a lot more than just PSR-4 autoloading if you want to be productive using this method, and want to have a lot of convience methods availabel to you.
That's where the package Laravel-modules comes in.
This package will give you the ability to have custom namespaces for views, config, and languages. Handling migrations/seeds per module. Assets management per module. Helper convience methods. And so much more.
This package is what AsgardCMS uses behind the scenes to achieve its modular approach.
One more thing
To top it all of, every module can be considered as a composer package. Meaning you can re-use your modules on other projects and handle its versioning easily.
This means that on top of having a maintainable architecture, you also save time be being able to re-use modules you created for other projects.
Convinced ? Check out the Laravel-modules package and give it a try.
Writing modular applications with laravel-modules的更多相关文章
- Coroutines declared with async/await syntax is the preferred way of writing asyncio applications. For example, the following snippet of code (requires Python 3.7+) prints “hello”, waits 1 second, and
小结: 1.异步io 协程 Coroutines and Tasks — Python 3.7.3 documentation https://docs.python.org/3/library/a ...
- 十 Writing YARN Applications
本节介绍: 使用yarn 高级提交写yarn应用程序.其实已经yarn底层API.MR计算框架对底层的API实现了封装. 高级提交指直接使用yarn的三种接口来提交应用程序: 1)YarnCl ...
- 4: 模块化应用程序开发 Modular Application Development Using Prism Library 5.0 for WPF (英汉对照版)
A modular application is an application that is divided into a set of loosely coupled functional uni ...
- Python Tutorial 学习(六)--Modules
6. Modules 当你退出Python的shell模式然后又重新进入的时候,之前定义的变量,函数等都会没有了. 因此, 推荐的做法是将这些东西写入文件,并在适当的时候调用获取他们. 这就是为人所知 ...
- Github上Laravel开源排行榜Star数61-90名
Github上Laravel开源排行榜Star数61-90名,罗列所有 Laravel 开源扩展包,含 Github Star 数量,下载数量和项目简介.默认排序是按Star数量从多到少来排 61.c ...
- Developing modules for the Apache HTTP Server 2.4
Developing modules for the Apache HTTP Server 2.4 Available Languages: en This document explains how ...
- 13 Stream Processing Patterns for building Streaming and Realtime Applications
原文:https://iwringer.wordpress.com/2015/08/03/patterns-for-streaming-realtime-analytics/ Introduction ...
- Writing a Simple YARN Application 从hadoop生态抽出yarn ,单独使用yarn
Apache Hadoop 2.9.1 – Hadoop: Writing YARN Applications https://hadoop.apache.org/docs/current/hadoo ...
- 转:分享13款PHP开发框架
文章来自于:http://mashable.com/2014/04/04/php-frameworks-build-applications/ Building software applicatio ...
随机推荐
- Java中的冒泡排序和选择排序
//冒泡排序 public class Test5 { public static void main(String[] args) { int[] arr = {12,2,25,89,5}; bub ...
- session of express
[session of express] 1.express-session 一个提供session功能库 npm install express-session --save var session ...
- chromedriver和chrome匹配的版本
- Python之-------基础数据类型
数据类型: 计算可以处理各种不同文件,图形,音频,视频,网页等各种各样的数据,不同的数据,需要定义不同的数据类型.在Python中,能够直接处理的数据类型有以下几种: 一:nubmer(数字) 1.1 ...
- apache服务器伪静态配置说明
apache服务器伪静态配置说明: 第一种 .如果是多城市版分类并且使用了城市二级域名即(多城市+多域名),请修改apache的配置文件,把以下代码添加到配置文件的最后一行即可,注意把qibosoft ...
- pta l2-13(红色警报)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805063963230208 题意:给n个顶点,m条边,问每次删 ...
- Maven 添加jar包到本地仓库
一.使用Maven命令安装jar包 前提:在windows操作系统中配置好了Maven的环境变量,怎么配置请自己百度,这里不介绍,可参考https://jingyan.baidu.com/articl ...
- C++求图任意两点间的所有路径
基于连通图,邻接矩阵实现的图,非递归实现. 算法思想: 设置两个标志位,①该顶点是否入栈,②与该顶点相邻的顶点是否已经访问. A 将始点标志位①置1,将其入栈 B 查看栈顶节点V在图中,有没有可以到达 ...
- ora-12899解决方法
在使用ORACLE的过程中,会出现各种各样的问题,各种各样的错误,其中ORA-12899就是前段时间我在将数据导入到我本地机器上的时候一直出现的问题.不过还好已经解决了这个问题,现在分享一下,解决方案 ...
- mute
mute - 必应词典 英[mjuːt] n.哑吧:沉默的人:[法律]拒绝答辩的被告人:鸟粪 adj.哑的:缄默无言的:(一时)说不出话的:(猎狗)不叫的 v.排泄:减弱…的声音:柔和…的色调 网络静 ...