Intro
In this article, I'm going to list down few conceptually similar concepts in Laravel and Django web frameworks.
Contents
- Architecture
- the console
- Starting a dev server
- Tinker vs shell
- blade template vs django templates
- routes vs urls
- sessions
- request and response objects
- Writing a custom command
- Migrations
Background
I'm a Django developer for most of my career. I recently got a chance to work with Laravel framework. This article is based on the two experiences combined.
Disclaimer
As we know, both frameworks are powered by two different programming languages. So, both of them tend to act like an extension of their parent framework. So there are a lot of differences and syntax favor-ism for sure.
This is article doesn't try to do a concrete evaluation of each concepts. Instead, the similarity felt by me is explained here in a semi-formal language.
Architecture
Laravel is an MVC framework. Django is an MTV framework. Below table lists a shorter description of what each letters stands for (note, I use Laravel concept/letter first then Django concept/letter).
Concept | Usage in Larvel & Django |
---|---|
M vs M | Used in respective ORMs(Eloquent ORM and Django ORM), represents Database objects, configure table name |
V vs T | Templates rendered into Response objects |
C vs V | Validation of data, returning Response objects |
Controller or View is decided by patterns in Routes / urls respectivly. Also, we can see both Routes / urls are having name attribute. This name attribute can be used in templates for retrieving actual url of required page.
The console
Both Laravel and Django have a console helpers to ease developement.
Laravel | Django |
---|---|
php artisan |
python manage.py |
As you can see both artisan
and manage.py
and are two files created by their project scaffolding. And both of them prints the help and list of commands.
Starting a dev server
Both Laravel and Django have a dev server. And to my surprise both points to same address by default ie. http://127.0.0.1:8000
Laravel | Django |
---|---|
php artisan serve |
python manage.py runserver |
Shell
Both Laravel and Django provide an interactive shell/REPL.
Laravel | Django |
---|---|
php artisan tinker |
python manage.py shell |
Blade template vs django templates
In fact, both templates differe a lot. This is due to the fact that, Blade templates are trying to adore PHP where as Django templates adore python.
Still we can a lot of similar things here and there
Laravel | Django |
---|---|
{{ $variable }} |
{{ variable }} |
@include('foo.bar') |
{% include "foo/bar.html" %} |
@extends('layouts.app') |
{% extends "layouts/app.html" %} |
@csrf |
{% csrf_token %} |
Routes vs urls
Conceptually routes and urls are a lot differ. Routes allow a lot of flexibilities like allowing selection of method, auth, throttling, middleware etc. But, urls only allow patterns.
Similarities includes option to pass parameters vis url pattern, a name for identifying particular pattern
Laravel | Django |
---|---|
Route::get('/user/{id}',... |
path('articles/<int:year>/' |
Route::get('/articles....)->name('articles') |
path('articles/', view, name='articles') |
routes('articles') |
{% urls 'articles' %} |
sessions and cookies
We can similarity how sessions and cookies accessed/set in Laravel and Django
Laravel | Django |
---|---|
$request->session()->get('key') |
request.session.get('key') |
$value = $request->cookie('name'); |
value = request.get_signed_cookie('name') |
Request and Response objects
Both Laravel and Django has concept of Rquest and Response objects with similar parts.
Laravel | Django |
---|---|
$value = $request->header('X-Header-Name'); |
value = request.header('X-Header-Name') |
$uri = $request->path(); |
path = request.path |
$method = $request->method(); |
method = request.method |
$file = $request->file('photo'); |
file = request.FILES['photo'] |
Writing a custom command
Despite of instructions to create commands differ. Both have concept of creating custom command, also supports passing arguements along with the command.
Migrations
Laravel and Django supports creation of database migrations. Both Laravel and Django create a migration table to track, what migrations are already run.
Laravel | Django |
---|---|
php artisan make:migration add_local_table |
python manage.py makemigrations |
php artisan migrate |
python manage.py migrate |
php artisan migrate --help |
python manage.py migrate --help |
--database[=DATABASE] The database connection to use |
--database DATABASE Nominates a database to synchronize. Defaults to the "default" database. |
--env[=ENV] The environment the command should run under |
--settings SETTINGS The Python path to a settings module, e.g. "myproject.settings.main" . |
Note, comparison between --env
and --settings
is not perfect. But, this has similar impacts in some cases like having different DATABASE, CACHING etc. in different environment like production/staging/local. Using django-environ will make Django more parity with --env
.
Conclusion
Laravel and Django has a lot of similar concepts.
Reference
If you are interested in learning any of those frameworks. Their official websites are the best place to refer. Here are my favorite resources for both.
Top comments (1)
Thank you. It's a big help.