FastAPI vs. Django vs. Laravel vs. Flask: How are routes organized?

- Andrés Cruz - ES En español

Video thumbnail

We are going to talk about route types or the ways we have to define our routes. For that, I am going to compare Flask, FastAPI, Django, and Laravel frameworks, which are the ones I actually work with. Basically, these are divided into two types.

  • Flask and FastAPI: In the case of Flask, we have blueprints, which are decorators that we place directly on the endpoint. On the other hand, FastAPI —which was inspired by Flask for also being a microframework— takes this to the next level. It uses decorators to define its endpoints, but it also allows specifying whether methods are synchronous or asynchronous and allows including validations directly in the route definition. In this case, FastAPI is considered a more modern framework because it allows grouping parameters in a more structured way than Flask.
# routes/products.py
from flask import Blueprint

products_bp = Blueprint('products', __name__, url_prefix='/products')

@products_bp.route('/<int:product_id>', methods=['GET'])
def get_product(product_id):
    return {"id": product_id, "name": "Laptop"}

# app.py (Registration)
from flask import Flask
from routes.products import products_bp

app = Flask(__name__)
app.register_blueprint(products_bp)

#--------------FASTAPI
# routers/products.py
from fastapi import APIRouter

router = APIRouter(prefix="/products", tags=["Products"])

@router.get("/{product_id}")
async def get_product(product_id: int):
    return {"id": product_id, "name": "Laptop"}

# main.py (Registration)
from fastapi import FastAPI
from routers.products import router as products_router

app = FastAPI()
app.include_router(products_router)
  • Django and Laravel: Django separates the routes file from the views logic. The implementation is located in files named urls.py, which work as modules within each application. Although the definition is similar to Flask and FastAPI (URI, method, and name), the disruptive thing here is that we handle a separate file so as not to "clutter" the endpoint. Laravel takes this to the next level with a much more structured organization, allowing chainings, prefixes, middlewares, controllers, and multiple grouping ways.
  • # products/urls.py (Inside the 'products' app)
    from django.urls import path
    from .views import get_product
    
    urlpatterns = [
        path('<int:product_id>/', get_product, name='product-detail'),
    ]
    
    # config/urls.py (Global routes file)
    from django.urls import path, include
    
    urlpatterns = [
        path('products/', include('products.urls')),
    ]

In Laravel, although we can define more, we mainly use routes for the web, for the console, and for REST APIs. What I like about this scheme is that, when the application is medium or large, having all the routes in a single file makes reading difficult. In this model, we have a central place to organize them, unlike Flask or FastAPI, where to find a route you have to check where it was specifically defined. The small disadvantage is that, obviously, we end up with more files (for example, in Django you would have a routes file per module or application).

// routes/api.php
use App\Http\Controllers\ProductController;
use Illuminate\Support\Facades\Route;

// Option 1: Route group with prefix
Route::prefix('products')->group(function () {
    Route::get('{id}', [ProductController::class, 'show']);
});

// Option 2: Controller Route Group (Even cleaner in Laravel 11/12)
Route::controller(ProductController::class)->prefix('products')->group(function () {
    Route::get('{id}', 'show');
});

Advantages and Disadvantages

The main problem we have with routes in frameworks like FastAPI or Flask is that readability becomes difficult if we want to quickly go to the definition of an endpoint. Since the structure is based on decorators, it is hard to remember where a specific route was defined without navigating through the entire module structure. In these cases, even in the official FastAPI extension, an additional tool is required to facilitate quick navigation, something that in Django or Laravel already comes "free" due to its architecture.

We can summarize the decorator-based approach (whether through Blueprints or a Routes-like object) with the following characteristics:

  • Advantages: Code proximity. The function and the route live together, making it easier to understand the context of the endpoint. Additionally, it allows having typing and automatic documentation cleanly.
  • Disadvantages/Risks: Disorder and coupling. By not having a rigid structure, it is easy to break conventions in long projects, such as parameter names or route structures, which hinders the onboarding of new developers.

On the other hand, the centralized and decoupled approach (like Laravel's) works as an additional layer of separation of concerns. Rules, logic, and structure are in separate files.

  • Advantages: It is much easier to maintain. You can change a URL or quickly analyze what routes exist to avoid duplications. For large teams, having a rigid structure where everything is in a single place greatly facilitates the management of many routes.
  • Disadvantages: Sometimes, file names in the folder structure can turn into a novel. For example, in an organized structure for Laravel, you could have files like post.list, post.save, or post.form. The problem with this approach is that, by using such generic names, the initial reading is clear because the pattern repeats, but it forces you to navigate between many files to find what you are looking for. If you want to locate the course listing, you have to look for the list.blade.php file and navigate manually because there is no direct way to reach the component.

This model has its pros and cons: it is more logical and the files are easier to organize, but it requires writing more lines of code and creates navigation friction. In contrast, Laravel offers an additional advantage by allowing the management of multiple route files per module (web, API, console, etc.), allowing specific routes for each case.

// Contact
Route::livewire('contact', 'pages::dashboard.contact.list')->name('contact-list');
Route::livewire('contact-services', 'pages::dashboard.contact.service-list')->name('contact-service-list');

Laravel uses a scheme on "steroids" that differentiates it from other frameworks like Django. By having more expressive methods, such as resource-type routes, they can be centralized and managed completely from a single place without having to define controllers in another layer if they are used directly from endpoints. The main disadvantage here is the strong dependency on the framework; if you decide to change technology, you will have to analyze and migrate this entire block.

Route::group(['prefix' => 'libros'], function () {
    Route::get('', [PostController::class, 'books'])->name('web-book-list');
    // Route::get('{post_url_clean}', [App\Http\Controllers\Courses\BookController::class, 'show'])->name('web-book-show');
    Route::get('{post:url_clean}', [PostController::class, 'show_book'])->name('web-book-show');
});

On the other hand, Laravel routes can be a mess if not well organized, as they allow multiple definitions and structures like redirects or callbacks. If not managed correctly, routes become kilometers long and hard to read, losing the advantages of the original scheme. If you want to dive deeper into these technologies, you can visit my academy platform to see step-by-step organized tutorials and access the source code.

Laravel, CodeIgniter, Django, Flask, FastAPI... They are the same....

Video thumbnail

Hello! In this post, I want to address a common question in web development: Is it worthwhile or necessary to learn development in multiple technologies? Although we will focus on web backend, this reflection applies to any area of software development.

The specific question is: Is it justified to learn frameworks like Flask, FastAPI, Django, CodeIgniter, Laravel, Symfony, or other similar ones?

1. The Value of Technological Diversity

Initially, the answer is a resounding YES. Learning multiple technologies greatly nourishes you as a developer.

A. Mitigating the Risk of Obsolescence

It is quite risky to focus on a single technology. If it disappears or loses relevance (as has happened with the boom of jQuery in certain niches), you are left with few job options.

General Recommendation: In general terms, I do recommend you develop in multiple technologies, especially if you focus on server-side web development.

B. Selecting the Best Framework per Project

Knowing several technologies gives you a better range of options. Once you master several of them (as I do with CodeIgniter, Laravel, Flask, FastAPI, and Django), you can select the most appropriate framework based on the specific requirements of the project:

  • Project Requirement    Recommended Option    Reason
  • Simple Application (Blog)    CodeIgniter, Flask, Laravel    Django might be too large, and FastAPI focuses on APIs.
  • CodeIgniter is light and fast for a simple PHP.
  • Creating REST APIs    FastAPI    It is focused and optimized for this purpose.
  • Chat Application (Full Duplex Communication)

If you only master one technology, you will be forced to develop all projects in that single tool, even if it is not the most efficient.

2. The Focus and the Common Point (MVC)

Although it is worthwhile to know several technologies, I also said that it "possibly isn't" worthwhile if it's not your main focus.

A. Prioritize a Niche

There are many technologies being born daily, and it's impossible to follow them all. You must focus on a niche. My fundamental niche is backend; that's why I extend into those frameworks. For the frontend (the client side), I only develop in Vue, for example, because Angular or React are not my main focus.

B. The Concept is What Matters: The MVC

Here comes the most important point to justify learning multiple frameworks:

  • CodeIgniter, Laravel, Django, Flask, etc., although they are different languages (PHP, Python), they all seek the same objective: to create web applications.
  • They all use (or variants of) the Model-View-Controller (MVC). Django uses the MTV (Model-Template-View), and Laravel extends it with its own components (Livewire).
  • If you know one of these technologies, you already have a solid conceptual basis to learn the rest, as you will see more similarities than differences.
  • Syntax vs. Concept: The real challenge is the syntax (switching from PHP to Python). But as a developer, you should focus on learning the concepts (how to work with primitives, arrays, petitions, requests, and databases).

Conclusion

The procedure and the scheme for implementing a web application (Controller, Model, database connection) are virtually identical. Therefore, mastering one will greatly facilitate mastering the next.

For this reason, I strongly recommend that if you want to develop or learn web development, you work with at least some of them, and that they are from different programming languages. Remember that I have several resources (courses on Udemy, books in my Academy) that can facilitate this path for you.

Laravel vs Django Translation Handling

Video thumbnail

Let's talk a little about how the translation process works in each of these frameworks.

The first thing I want to say is that, for example, translations in PHP are generally more or less the same as those we do in Laravel. In Django, the logic is also similar: we'll always have a file where we store the key and the corresponding translation.

Differences in syntax

In PHP, the process is a little more straightforward because it's less jargon-intensive. It's basically a JSON file containing the key and value:

resources\lang\es.json

{
   "Light": "Claro",
   "Dark": "Oscuro",
   ***
}

resources\lang\en.json

{
   "Light": "Light",
   "Dark": "Dark",
    ***
}

In Django, as is often the case in Python, everything is a bit more verbose. It's not complicated, but it is more detailed.

The idea here is to discuss the steps we need to follow in each case to correctly generate translations.

Translations in Django (Python web in general)

In Django, it all starts by scanning the files where we use translation keys.
This applies to both templates and views (which in PHP would be controllers).

  • Functions like gettext (which is native to Python) or tags like {% trans %} are used.
    In the code, we can also use _('text') to indicate what we want to translate.
  • The process, however, is more laborious:
  • Install the gettext utility.
  • Create specific configurations to specify the translation folder.
  • Run commands that generate .po files.
  • Manually create the locale folder in each application.
  • Configure exceptions (for example, ignore files like requirements.txt).

The big advantage here is that Django automatically scans all files and generates the keys for us.
Additionally, the .po files include comments indicating where each key is used, which is very useful for reference.

Translations in Laravel (PHP in general)

In Laravel, the process is much simpler:

  • Manually create the resources/lang folder.
  • Create the language files (en.json, es.json, etc.).
  • Write the keys and values ​​one by one.

Here, the work is manual and somewhat tedious.
We can rely on AI tools like Copilot, Claude, or Gemini to automate it, but in the end, it's still a process of writing translations.

Changing languages ​​is done with simple functions such as:

App::setLocale('es');

Comparison

Laravel (PHP):

  • Advantage → Simple and straightforward process.
  • Disadvantage → It's manual; you have to enter all the keys by hand.
  • Django (Python):
  • Advantage → The system automatically scans and generates the keys.
  • Disadvantage → It's more laborious, requiring installation of utilities and additional configuration.

Conclusion

Although the process is faster in Laravel, I find Django more robust because it automatically scans and adds references to where each translation is used.

That's why, in this case, I give the point to Django (Python web in general).
I find it a more reliable solution, even if it's a bit more work at first.

The advantages and disadvantages of two main approaches to route definition are analyzed: direct coupling (where the route and the endpoint are co-located) and the decoupled (centralized) approach.


Únete a la comunidad de desarrolladores que han decidido dejar de picar código y empezar a construir productos reales. Recibe mis mejores trucos de arquitectura cada semana:

I agree to receive announcements of interest about this Blog.