Masonite 2.0

Last updated 4 months ago

Masonite 2 brings an incredible new release to the Masonite family. This release brings a lot of new features to Masonite to include new status codes, database seeding, built in cron scheduling, controller constructor resolving, auto-reloading server, a few new internal ways that Masonite handles things, speed improvements to some code elements and so much more. We think developers will be extremely happy with this release.

Upgrading from Masonite 1.6 to Masonite 2.0 shouldn't take very long. On an average sized project, this upgrade should take around 30 minutes. We'll walk you through the changes you have to make to your current project and explain the reasoning behind it.

Controller Constructors

Controller constructors are now resolved by the container so this removed some redundancy within your code and any duplicated auto resolving can now be directly in your constructor:

from masonite.request import Request
class YourController:
def __init__(self, request: Request):
self.request = Request
def show(self):
print(self.request) # <class masonite.request.Request>

Read more in the Controllers documentation.

Tinker Command

There is a new command that starts a Python shell and imports the container for you already. Test it out to verify that objects are loaded into your container correctly. It's a great debugging tool.

$ craft tinker

Read more in The Craft Command Introduction documentation.

Show Routes Command

Masonite 2 ships with an awesome little helper command that allows you to see all the routes in your application

$ craft show:routes

Read more in The Craft Command Introduction documentation.

Server Reloading

A huge update to Masonite is the new --reload flag on the serve command. Now the server will automatically restart when it detects a file change. You can use the -r flag as a shorthand:

$ craft serve -r

Read more in The Craft Command Introduction documentation.

Autoloading

An incredible new feature is autoloading support. You can now list directories in the new AUTOLOAD constant in your config/application.py file and it will automatically load all classes into the container. This is great for loading command and models into the container when the server starts up.

You can also use this class as a standalone class in your own service providers.

Read more in Autoloading documentation.

Updated Libraries

Updated all libraries to the latest version with the exception of the Pendulum library which latest version is a breaking change and therefore was left out. The breaking change would not be worth it to add the complexity of upgrading so you may upgrade on a per project basis.

Removed Importing Duplicate Class Names

Previously you had to import classes like:

from masonite.drivers.UploadDriver import UploadDriver

Now you can simply specify:

from masonite.drivers import UploadDriver

Because of this change we no longer need the same duplicated class names in the PROVIDERS list either.

Read more about changing duplicated class names under the Duplicate Class Names documentation.

Redirection Provider

Removed the need for the redirection provider completely. You need to remove this from your PROVIDERS list.

Redirection

Renamed Request.redirectTo to Request.redirect_to

Also removed the .send() method and moved the dictionary into a parameter:

def show(self):
return request().redirect('/dashboard/@id', {'id': '5'})

Read more in the Requests documentation.

Request Only

Added a new Request.only method to fetch only specific inputs needed.

Read more in Requests documentation.

Get Request Method

Added a new Request.get_request_method() method to the Request class.

Read more in Requests documentation.

New Argument in Request.all

You can now completely remove fetching of any inputs that Masonite handles internally such as __token and __method when fetching any inputs. This is also great for building third party libraries:

Request.all(internal_variables=False)

Read more in Requests documentation.

Made several changes to the CSRF Middleware

Because of the changes to internal framework variables, there are several changes to the CSRF middleware that comes in every application of Masonite.

Be sure to read the changes in the Upgrade Guide 1.6 to 2.0.

Added Scheduler to Masonite

Added a new default package to Masonite that allows scheduling recurring tasks:

Read about Masonite Scheduler under the Task Scheduling documentation.

Added Database Seeding Support

It's important during development that you have the ability to seed your database with dummy data. This will improve team development with Masonite to get everyones database setup accordingly.

Read more in the Database Seeding documentation.

Added a New Static File Helper

Now all templates have a new static function in them to improve rendering of static assets

Read more in the Static Files documentation.

Added a New Password Helper

You can use the password helper to hash passwords more simply than using straight bcrypt:

from masonite.helpers import password
password('secret') # returns bcrypt password

Read more in the Encryption documentation.

Added Dot Notation To Upload Drivers And Dictionary Support To Driver Locations.

You can now specify which location in your drivers you want to upload to using a new dot notation:

Upload.store(request().input('file'), 'disk.uploads')

This will use the directory stored in:

DRIVERS = {
'disk': {
'uploads': 'storage/uploads',
'profiles': 'storage/static/users/profiles/images'
},
...
}

Read more in the Uploading documentation.

Added Status Code Provider

Masonite 2 removes the bland error codes such as 404 and 500 errors and replaces them with a cleaner view. This also allows you to add custom error pages.

Read more in the Status Codes documentation.

Added Explicitly Imported Providers

Providers are now explicitly imported at the top of the file and added to your PROVIDERS list which is now located in config/providers.py. This completely removes the need for string providers and boosts the performance of the application sustantially