from masonite.helpers import appapp().make("session")
You can also resolve dependencies directly and even pass arguments when resolving:
from masonite.helpers import appapp("session")#== Sessionapp("my_service", args)
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:
from masonite.facades import Dumptest =1data ={"key":"value"}Dump.dump(test, data)# ORdump(test, data)
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:
from masonite.facades import Dumptest =1data ={"key":"value"}Dump.dd(test, data)# ORdd(test, data)
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
from masonite.middleware import ClearDumpsBetweenRequestsMiddlewareclassKernel: http_middleware = [#... ClearDumpsBetweenRequestsMiddleware ]
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.
from masonite.utils.location import base_pathbase_path()# /Users/johndoe/my-project/base_path("storage/framework")# /Users/johndoe/my-project/storage/framework
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.
from masonite.utils.location import views_pathviews_path()# /Users/johndoe/my-project/templatesviews_path("admin/index.html")# /Users/johndoe/my-project/templates/admin/index.html
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.
from masonite.utils.location import controllers_pathcontrollers_path()# /Users/johndoe/my-project/app/controllerscontrollers_path("admin/AdminController.py")# /Users/johndoe/my-project/app/controllers/admin/AdminController.py
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.
from masonite.utils.location import mailables_pathmailables_path()# /Users/johndoe/my-project/app/mailablesmailables_path("WelcomeUser.py")# /Users/johndoe/my-project/app/mailables/WelcomeUser.py
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.
from masonite.utils.location import config_pathconfig_path()# /Users/johndoe/my-project/configconfig_path("custom.py")# /Users/johndoe/my-project/config/custom.py
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.
from masonite.utils.location import migrations_pathmigrations_path()# /Users/johndoe/my-project/databases/migrationsmigrations_path("2022_11_01_043202_create_users_table.py") # /Users/johndoe/my-project/databases/migrations/2022_11_01_043202_create_users_table.py
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.
from masonite.utils.location import seeds_pathseeds_path()# /Users/johndoe/my-project/databases/seedsseeds_path("products_table_seeder.py")# /Users/johndoe/my-project/databases/seeds/products_table_seeder.py
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.
from masonite.utils.location import jobs_pathjobs_path()# /Users/johndoe/my-project/app/jobsjobs_path("SendInvoices.py")# /Users/johndoe/my-project/app/jobs/SendInvoices.py
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.
from masonite.utils.location import resources_pathresources_path()# /Users/johndoe/my-project/resourcesresources_path("css/app.css")# /Users/johndoe/my-project/resources/css/app.css
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.
from masonite.utils.location import models_pathmodels_path()# /Users/johndoe/my-project/app/modelsmodels_path("Product.py")# /Users/johndoe/my-project/app/models/Product.py
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.
from masonite.utils.location import models_pathmodels_path("blog/Article.py", absolute=False)# app/models/blog/Article.py
URLs and Routes
url
The Url helper allows you to create a full path URL:
from masonite.helpers import urlurl.url("/dashboard")#== example.com/dashboard
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:
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:
if request.user()and request.user().admin ==1:#.. do something
with the optional helper you can condense the code into something like this:
from masonite.helpers import optionalifoptional(request.user()).admin ==1:#.. do something