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.
All helpers can be imported from helpers
module.
Masonite Specifics
app
Get easily access to application container:
You can also resolve dependencies directly and even pass arguments when resolving:
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:
This will dump data in console in a nice format to ease debugging.
dd
If you want the code to stop and renders a dump page instead you can use the dump and die helper named dd
:
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.
config
TODO
env
TODO
Paths
base_path
Get the absolute path to your project root directory or build the absolute path to a given file relative to the project root directory.
views_path
Get the absolute path to your project views directory or build the absolute path to a given file relative to the project views directory.
controllers_path
Get the absolute path to your project controllers directory or build the absolute path to a given file relative to the project controllers directory.
mailables_path
Get the absolute path to your project mailables directory or build the absolute path to a given file relative to the project mailables directory.
config_path
Get the absolute path to your project config directory or build the absolute path to a given file relative to the project config directory.
migrations_path
Get the absolute path to your project migrations directory or build the absolute path to a given file relative to the project migrations directory.
seeds_path
Get the absolute path to your project seeds directory or build the absolute path to a given file relative to the project seeds directory.
jobs_path
Get the absolute path to your project jobs directory or build the absolute path to a given file relative to the project jobs directory.
resources_path
Get the absolute path to your project resources directory or build the absolute path to a given file relative to the project resources directory.
models_path
Get the absolute path to your project models directory or build the absolute path to a given file relative to the project models directory.
All above paths helper return an absolute path to the location. When providing a file path to the helper you can set absolute=False
to get the path relative given directory.
URLs and Routes
url
The Url helper allows you to create a full path URL:
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:
asset
You can also generate a URL for an asset:
route
You can also generate a URL for a route by its route name:
You can also generate just a path:
It accepts a dictionary to add query string parameters when building the route url:
mix
TODO
Python Helpers
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:
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:
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:
with the optional helper you can condense the code into something like this:
collect
TODO
flatten
TODO
Last updated