Masonite Documentation
v4.0
v4.0
  • Introduction and Installation
  • Prologue
    • Creating A Blog Tutorial
    • Release Cycle
    • Contributing Guide
    • How To Contribute
  • The Basics
    • Routing
    • Controllers
    • Middleware
    • Response
    • Request
    • Static Files
    • Views
    • Environments
    • Configuration
    • Error Handling
  • Features
    • API Development
    • Authentication
    • Authorization
    • Broadcasting
    • Caching
    • Compiling Assets
    • Commands
    • CSRF Protection
    • Events
    • Facades
    • Filesystem and Uploading
    • Hash ID's
    • Helpers
    • Mail
    • Notifications
    • Package Development
    • Queues and Jobs
    • Rate Limiting
    • Sessions
    • Task Scheduling
    • Tinker Shell (REPL)
    • Validation
  • Architecture
    • Service Providers
    • Service Container
  • Security
    • CORS
    • Hashing
  • Masonite ORM
    • To Masonite ORM Docs
  • Testing
    • Getting Started
    • HTTP Tests
    • Database Tests
    • Commands Tests
    • Console Tests
    • Mocking
    • Extending
  • Official Packages
    • Masonite Debugbar
  • How-to Guides
    • Handling AJAX requests with expired authentication
    • Build Email Verification from Scratch With Masonite Framework and JSON Web Tokens
    • Deploying a Masonite Application to Heroku
    • How To Deploy Masonite to PythonAnywhere
    • How-To: Use RabbitMQ with Masonite 2.0 queues
    • How To Use The Repository Pattern with Masonite
    • Making Masonite and Laravel Mix work together
  • What's New
    • Masonite 1.3
    • Masonite 1.4
    • Masonite 1.5
    • Masonite 1.6
    • Masonite 2.0
    • Masonite 2.1
    • Masonite 2.2
    • Masonite 2.3
    • Masonite 3.0
  • Upgrade Guide
    • Masonite 1.3 to 1.4
    • Masonite 1.4 to 1.5
    • Masonite 1.5 to 1.6
    • Masonite 1.6 to 2.0
    • Masonite 2.0 to 2.1
    • Masonite 2.1 to 2.2
    • Masonite 2.2 to 2.3
    • Masonite 2.3 to 3.0
    • Masonite 3.0 to 4.0
Powered by GitBook
On this page
  • Overview
  • Built-in Facades
  • Creating Your Own Facades
Edit on GitHub
Export as PDF
  1. Features

Facades

PreviousEventsNextFilesystem and Uploading

Last updated 3 years ago

Facades are an easy way to access the classes without making them from the .

Overview

Facades are just a shortcut to resolve a key in the . Instead of doing:

application.make("mail").send()

you can do:

from masonite.facades import Mail

Mail.send()

To import any built-in facades you can import them from the masonite.facades namespace:

from masonite.facades import Request

def show(self):
    avatar = Request.input('avatar_url')

Built-in Facades

Masonite ships with several facades you can use out of the box:

  • Auth

  • Broadcast

  • Config

  • Dump

  • Gate

  • Hash

  • Loader

  • Mail

  • Notification

  • Queue

  • Request

  • Response

  • Session

  • Storage

  • View

  • Url

Creating Your Own Facades

To create your own facade is simple:

from masonite.facades import Facade

class YourFacade(metaclass=Facade):
  key = 'container_key'

Then import and use your facade:

from app.facades import YourFacade

YourFacade.method()

To benefit from code intellisense/type hinting when using Facade (e.g. in VSCode editor) you should create a .pyi file just next to your facade file class to add type hinting to your new facade.

Then in this facade you should declare your typed methods. Here is a partial example of the Mail facades types hints:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from ..mail import Mailable

class Mail:
    """Handle sending e-mails from Masonite via different drivers."""

    def mailable(mailable: "Mailable") -> "Mail":
        ...
    def send(driver: str = None):
        ...

Notice that:

  • methods code is not present, but replaced with ....

  • methods do not receive self as first argument. Because when calling the facade we don't really instantiate it (even if we get an instance of the object bound to the container). This allow to have the correct type hint behaviour.

Service Container
Service Container
Service Container