Masonite as an alternative to Laravel in Python
Tags: programming, webdev, python
Published: 2024-03-03 01:36:12

Since I became a developer I wanted to try new languages, frameworks and different kind of software, just to learn something new or because I saw someone using it and making incredible things with it.

The most difficult thing to me was to find a proper way to develop a web application because I only knew a little of html and I was starting to learn PHP, and everything I did was messy and hard to understand, until I met Laravel.

[Laravel](https://laravel.com/) is a good-known framework is pretty friendly with beginners because of their structure, their community and all the documentation that is available for us to look up. Also, it's a good framework for production environments thanks to their ecosystem so you could easily build huge appliances and deploy them to production.

I became to use a lot Laravel when I fell in love with [Laravel Nova](https://nova.laravel.com/). Have you ever struggle creating a CRUD over and over for every project you worked on? I don't know a web application that doesn't have a CRUD. Even to manage the users table, you always need to manage something in DB. 

Laravel Nova is one of the best companion – along with [FilamentePHP](https://filamentphp.com/) – because both of them let you create admin dashboards in minutes. 

### PHP receives too much hate

Sadly PHP is a hated language because people often thinks about older versions (like 5.5 or 5.6) when it had a lot to improve and people were doing spaghetti code everywhere with it. I have listened many clients say it's a garbage language and eventually I got tired. 

I decided to find another option in another language.

I searched the web comparing web frameworks to find a replacement to my current stack, a good framework with a great community and (if possible) similar to Laravel in terms of syntax and features.

Different options appeared in front of me that I tried but only one was good enough to make me switch my stack: [Masonite Framework](https://docs.masoniteproject.com/)

### Python and Masonite Framework 

Scripting in Python is something that I might do everyday. For me is the fastest language to make whatever you need to do. From creating a simple call using `requests` to build a complete project with Routing, Models, Authorization, Controllers and everything we need as developers to make a web project work.

The structure and the syntax that uses Masonite Framework is similar enough that you really don't need to thing so much to begin working in your project. 

Here is a couple examples to compare the frameworks.


Controllers comparison:

**Laravel controller**
```php
<?php
 
namespace App\Http\Controllers;
 
use App\Models\User;
use Illuminate\View\View;
 
class UserController extends Controller
{
    /**
     * Show the profile for a given user.
     */
    public function show(string $id): View
    {
        return view('user.profile', [
            'user' => User::findOrFail($id)
        ]);
    }
}
```
**Masonite controller**
```python
"""A WelcomeController Module."""

from masonite.controllers import Controller
from masonite.request import Request
from masonite.response import Response
from masonite.views import View

class WelcomeController(Controller):
    """WelcomeController Controller Class."""

    def index(self, request: Request, view: View):
        id = request.param('id')
        return view.render("user.profile", {
            'user': User.find_or_fail(id)
        })
```

Models comparison:

 **Laravel model**
```php
<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
 
class Post extends Model
{
    // ...
}
```
**Masonite model**
```python
"""Post Model."""
from masoniteorm.models import Model

class Post(Model):
    pass
```


Commands, migrations, routing, middlewares, providers, it has everything. For every feature I could give an alternative in Masonite. 

Laravel has a huge community and to me **is the best framework** written in PHP to create web applications and if you love it as much as I do and you know some Python, you should give it a try.

If you want to learn more, I suggest you to visit the [Official Masonite Documentation](https://docs.masoniteproject.com/).