Is it worth, for a Django developer to switch to Laravel?
By: Deniz Jusufović
Tags: PHP, Php Developers, Python, Django, Laravel

Differences

The first thing a developer has to ask himself is:

“What are the goals of the application I’m writing”?

Unless the application has to use advanced computations, time-series processing, prediction systems, document classification etc. I would recommend using Laravel, as you would spend much less time in the setup of your application, and be able to start writing your business logic sooner.

URL dispatcher

The URL dispatcher is centralized and really easy to follow. Unlike in Django, I find it much easier to follow the routing syntax of Laravel. In Django, the routes are still written as regex, which many developers find harder to write.

First, we define the URL as a regex expression, then we define which methods will handle the specific request (The POST request will be handled by the update_customer method, etc..).

Unnecessarily complicated URL definition I would say.

What I really love in Laravel, is the Route Model Binding. In the second example, you can see that instead of the id as a URL parameter I’ve written “customer” (you can name it anything you like). If we type hint the parameter in the show method in the CustomerController as Customer, the Route Model Binding will fetch the Customer with that id from the database. Crazy right?

MVC vs MTV

The first thing to emphasize is that the pattern is not different between the frameworks, but rather the naming! As I mentioned in the similarities section, both frameworks are using the Model-View-Controller pattern. This pattern in Django is called (Model-Template-View) MTV, where the Template is the same as the View, and the View is where the business logic lives (Controller in Laravel).

Models and Migrations

The process of creating/amending the database differs from one framework to another, but this seems to be the one thing that was really hard for me to get used to.

The whole process in Django starts by creating the Model and defining all fields that you would like that model to have. You define the type of fields, constraints, validations, and you can override methods like save, etc.

After defining your model, all you have to do is run python manage.py makemigrations, and immediately a migration is created for the Customer model. When you decide to change or add a field in the database, you just have to change/add that field in your model, and run the command again.

On the other hand, with Laravel, these two things are separate. You define your Model and relations, but you have to create your migrations separately. This way, you can never know what your database table contains by looking at your model.

Hands down, one of the best features of the Django framework.

Admin Site

Django supports Administration Graphical user interface which is ready to use almost right after you create a model (with just a bit more configuration). It reads metadata from the models and provides a quick interface where trusted users can manage content on the site.

Observers and Requests

As I mentioned above, in Django you override methods and define validation directly in the model. That adds a lot of code which, I would agree, does not belong there. Laravel resolves this in a very clean way.

The model event listeners are separated into so-called Observers, where the wanted logic occurs.

The validation is set to be in the so-called Requests. They are simple classes that define the rules of the request validation.

There are a lot of other differences like Django’s decorators and mixins, and Laravel’s traits, rules, resources, etc. that you should consider taking a look at.

In my opinion, it could be a bit more difficult for a developer to switch from Laravel to Django because it might look like a more chaotic framework. But if you have enough experience in web development and you know the principles of the MVC pattern, there’s nothing that can stop you from switching to any framework.

Challenges

It’s always a challenge for a person to get out of his/her comfort zone, and do something different. Of course, changing what you do can be tough, but for me it was actually fairly easy to switch to Laravel. All I had to do is open the documentation and follow it through the process. What I was surprised to see is that the creators of the framework also have an awesome tutorial series which helped me a lot.

In my opinion, the most challenging part was to convince myself that I really want to switch from technology after spending many years specializing in it.

Learning resources

There are a lot of tutorials and blogs that I’ve been using the Php/Laravel documentation and following the Laracasts lectures since the start and I’m 100% satisfied. Here are some useful links for starters:

Laracast

Laravel official documentation

Laravel API

Traversity Media Tutorials

Useful tips

In the beginning, it is crucial not to jump straight to learning Laravel, but instead learning PHP. If you have had no experience in PHP before, just take a look at it, write some utility scripts first, get comfortable with it. Easing into it, and not putting too much pressure on yourself at once will make you a better developer, in the end, no matter which framework or language it is.

Language switching is a normal practice in the era of code mastery evolution. Without any doubt, switching to PHP and Laravel was a life-changing experience for me and helped me sharpen my mind and broaden my horizons.

Zero regrets!