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的更多相关文章

  1. 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 ...

  2. 十 Writing YARN Applications

    本节介绍:     使用yarn 高级提交写yarn应用程序.其实已经yarn底层API.MR计算框架对底层的API实现了封装. 高级提交指直接使用yarn的三种接口来提交应用程序: 1)YarnCl ...

  3. 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 ...

  4. Python Tutorial 学习(六)--Modules

    6. Modules 当你退出Python的shell模式然后又重新进入的时候,之前定义的变量,函数等都会没有了. 因此, 推荐的做法是将这些东西写入文件,并在适当的时候调用获取他们. 这就是为人所知 ...

  5. Github上Laravel开源排行榜Star数61-90名

    Github上Laravel开源排行榜Star数61-90名,罗列所有 Laravel 开源扩展包,含 Github Star 数量,下载数量和项目简介.默认排序是按Star数量从多到少来排 61.c ...

  6. 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 ...

  7. 13 Stream Processing Patterns for building Streaming and Realtime Applications

    原文:https://iwringer.wordpress.com/2015/08/03/patterns-for-streaming-realtime-analytics/ Introduction ...

  8. Writing a Simple YARN Application 从hadoop生态抽出yarn ,单独使用yarn

    Apache Hadoop 2.9.1 – Hadoop: Writing YARN Applications https://hadoop.apache.org/docs/current/hadoo ...

  9. 转:分享13款PHP开发框架

    文章来自于:http://mashable.com/2014/04/04/php-frameworks-build-applications/ Building software applicatio ...

随机推荐

  1. JDBC的基本概念

    英文名:Java DataBase Connectivity 中文名:数据库连接 作用: java操作数据库 本质上(sun公司的程序员)定义的一套操作关系型数据库的规则也就是接口,各数据库厂商实现接 ...

  2. java中File类的常用方法总结

    java中File类的常用方法 创建: createNewFile()在指定的路径创建一个空文件,成功返回true,如果已经存在就不创建,然后返回false. mkdir() 在指定的位置创建一个此抽 ...

  3. 无线渗透wep加密路由器

    停掉网络服务 service network-manager stop 检查现在的环境适不适合使用airmon-ng airmon-ng check 杀死可能冲突的进程 开启网卡monitor模式 a ...

  4. 项目没有build path问题(转)

    感谢作者分享:https://blog.csdn.net/u012572815/article/details/76353018 问题1.通过eclipse的svn资源库添加的项目,显示的方式和直接创 ...

  5. Python 继承与多继承

    相关知识点: __class__.__name__的用法. >>> class ABC: def func(self): print('打印类名:',__class__.__name ...

  6. python2和python3 切换

    转帖-[官解]Windows上Python2和3如何兼容 想学习Python3,但是暂时又离不开Python2.在Windows上如何让它们共存呢? 目前国内网站经常会让大家把其中一个python.e ...

  7. AI图谱

  8. 分组背包模板题 hdu1712

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 第一次接触分组背包,参考博客:https://blog.csdn.net/yu121380/ar ...

  9. Selenium 定位元素原理,基本API,显示等待,隐式等待,重试机制等等

    Selenium  如何定位动态元素: 测试的时候会遇到元素每次变动的情况,例如: <div id="btn-attention_2030295">...</di ...

  10. Javascript(es2016) import和require用法和区别

    http://blog.csdn.net/chinaycheng/article/details/52559439 其中这个总结很到位: 加载方式 规范 命令 特点 运行时加载 CommonJS/AM ...