Helpers
Masonite has several helpers available. Many helpers are used internally to help write clean Masonite code but can be used in your projects as well.

Url

Url

The Url helper allows you to create a full path URL:
1
from masonite.helpers import url
2
3
url.url("/dashboard") #== example.com/dashboard
Copied!
The URL will come from the APP_URL in your application config file.
It accepts a dictionary to add query string parameters when building the url:
1
url.url("/search/users", query_params={"search": "John Doe" "order": "asc"})
2
#== http://masonite.app/users/?search=John%2Doe&order=asc
Copied!

Asset

You can also generate a URL for an asset:
1
url.asset("s3.invoices", "invoice-01-2021.pdf")
Copied!

Route

You can also generate a URL for a route by its route name:
1
url.route("users.profile", {"id": 1}) #== http://masonite.app/users/1/profile/
Copied!
You can also generate just a path:
1
url.route("users.profile", {"id": 1}, absolute=False) #== /users/1/profile/
Copied!
It accepts a dictionary to add query string parameters when building the route url:
1
url.route("user.profile", {"id": 1}, query_params={"preview": 1})
2
#== http://masonite.app/users/1/profile/?preview=1
Copied!

Compact

The compact helper is a shortcut helper when you want to compile a dictionary from variables.
There are times when you will have instances like this:
1
from masonite.view import View
2
3
def show(self, view: View):
4
users = User.all()
5
articles = Article.all()
6
return view.render('some.view', {"users": users, "articles": articles})
Copied!
Notice we repeated the users and articles key the same as the variables name. In this case we can use the compact helper to clean the code up a bit:
1
from masonite.view import View
2
from masonite.helpers import compact
3
4
def show(self, view: View):
5
users = User.all()
6
articles = Article.all()
7
return view.render('some.view', compact(users, articles))
Copied!

Optional

The optional helper takes an object and allows any method calls on the object. If the method exists on the object it will return the value or else it will return None:
You may have a peice of code that looks like this:
1
if request.user() and request.user().admin == 1:
2
#.. do something
Copied!
with the optional helper you can condense the code into something like this:
1
from masonite.helpers import optional
2
3
if optional(request.user()).admin == 1:
4
#.. do something
Copied!

Dump

You can easily dump variables into console for debugging, from inside a controller for example: For this you can use Dump facade or the built-in dump python method:
1
from masonite.facades import Dump
2
3
test = 1
4
data = {"key": "value"}
5
Dump.dump(test, data)
6
7
# OR
8
9
dump(test, data)
Copied!
This will dump data in console in a nice format to ease debugging.

Dump and Die

If you want the code to stop and renders a dump page instead you can use the dump and die helper named dd:
1
from masonite.facades import Dump
2
3
test = 1
4
data = {"key": "value"}
5
Dump.dd(test, data)
6
7
# OR
8
9
dd(test, data)
Copied!
This will stop code at this line and renders a nice dump page where you can see all variables dumped until now.
Note that dumps will accumulate into session. If you want to clear dumps, you can use Dump.clear() or you can enable the HTTP middleware ClearDumpsBetweenRequestsMiddleware to clear dumps between every requests.
Kernel.py
1
from masonite.middleware import ClearDumpsBetweenRequestsMiddleware
2
3
class Kernel:
4
5
http_middleware = [
6
#...
7
ClearDumpsBetweenRequestsMiddleware
8
]
Copied!