Masonite 2.2
Previously you had to append all routes with a
/
character. This would look something like:Get('/some/url')
You can now optionally prefix this without a
/
character:Get('some/url')
Previously we had to do something like:
def show(self, view: View, request: Request):
user = User.find(request.param('user_id'))
return view.render('some.template', {'user': user})
Now we can optionally get the parameter from the method definition:
def show(self, user_id, view: View):
user = User.find(user_id)
return view.render('some.template', {'user': user})
This is used as a wrapper around I/O operations. It will also be a wrapper around the upload drivers and moving files around and other file management type operations
We can now specify directly in the configuration file whether or not the threading or multiprocessing for the async type operations.
We added 4 new HTTP verbs:
HEAD
, CONNECT
, OPTIONS
, TRACE
. You import these and use them like normal:from masonite.routes import Connect, Trace
ROUTES = [
Connect('..'),
Trace('..'),
]
If the incoming request is a JSON request, Masonite will now return all errors as JSON
{
"error": {
"exception": "Invalid response type of <class 'set'>",
"status": 500,
"stacktrace": [
"/Users/joseph/Programming/core/bootstrap/start.py line 38 in app",
"/Users/joseph/Programming/core/masonite/app.py line 149 in resolve",
"/Users/joseph/Programming/core/masonite/providers/RouteProvider.py line 92 in boot",
"/Users/joseph/Programming/core/masonite/response.py line 105 in view"
]
}
}
This is more of an internal change for Core itself.
Before we had to specify that we wanted the server to auto-reload by specifying a -r flag:
$ craft serve -r
Now we can just specify the serve command it will default to auto-reloading:
$ craft serve
You can now specify it to NOT auto-reload by passing in 1 of these 2 commands:
$ craft serve -d
$ craft serve --dont-reload
By default you can only upload image files because of security reasons but now you can disable that by doing an accept('*') option:
def show(self, upload: Upload):
upload.accept('*').store(request.input('file'))
We moved from pytest to unittests for test structures.
Added a new
DatabaseTestcase
so we can properly setup and teardown our database. This works for sqlite databases by default to prevent your actual database from being destroyed.Before in templates we had to specify a path to go back to but most of the time we wanted to go back to the current path.
Instead of:
<form ..>
{{ back(request().path) }}
</form>
We can now do:
<form ..>
{{ back() }}
</form>
We built a new validation library from scratch and completely ripped out the old validation code. Any current validation code will need to be updated to the new way.
Previously we needed to pass in the request object to the Auth class like this:
from masonite.auth import Auth
from masonite.request import Request
def show(self, request: Request):
Auth(request).login(..)
Now we have it a bit cleaner and you can just resolve it and the request class will be injected for you
from masonite.auth import Auth
from masonite.request import Request
def show(self, request: Request, auth: Auth):
auth.login(..)
You may not notice anything but now if you bind a class into the container like this:
from masonite.auth import Auth
def register(self):
self.app.bind('Auth', Auth)
It will be resolved when you resolve it:
from masonite.auth import Auth
def show(self, auth: Auth):
auth.login(..)
This is why the Auth class no longer needs to accept the request class. Masonite will inject the request class for you when you resolve the class.
This works with all classes and even your custom classes to help manage your application dependencies
You can now do something like:
from masonite.auth import Auth
def show(self, auth: Auth):
auth.register({
'name': 'Joe',
'email': '[email protected]',
'password': 'secret'
})
Previously, each route's regex was being compiled when Masonite checked for it but we realized this was redundant. So now all route compiling is done before the server starts.
This has given Masonite a bit of a speed boost.
Masonite now has the ability to remember the previous container bindings for each object. This can speed of resolving your code by 10-15x. This is disabled by default as it is still not clear what kind of issues this can cause.
This is scheduled to be set by default in the next major version of Masonite
Now instead of doing this:
from masonite.request import Request
from masonite.validation import Validator
def show(self, request: Request, validate: Validator):
errors = request.validate(
validate.required('user')
)
if errors:
request.session.flash('errors', errors)
return request.back()
we can now shorten down the flashing of errors and do:
from masonite.request import Request
from masonite.validation import Validator
def show(self, request: Request, validate: Validator):
errors = request.validate(
validate.required('user')
)
if errors:
return request.back().with_errors(errors)
Last modified 1yr ago