Masonite Documentation
v2.0
v2.0
  • Introduction
  • Prologue
    • Contributing Guide
    • How To Contribute
    • Release Cycle
    • Known Installation Issues
    • Sponsors
  • What's New
    • Masonite 1.3
    • Masonite 1.4
    • Masonite 1.5
    • Masonite 1.6
    • Masonite 2.0
    • Masonite 2.1
  • 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
  • The Basics
    • Routing
    • Controllers
    • Views
    • Requests
    • Static Files
    • Helper Functions
  • The Craft Command
    • Introduction
    • Creating Commands
  • Architectural Concepts
    • Request Lifecycle
    • Service Providers
    • Service Container
  • Advanced
    • Middleware
    • Validation
    • Creating Packages
    • Extending Classes
    • Creating a Mail Driver
    • Sessions
    • Autoloading
    • Status Codes
    • Database Seeding
  • Useful Features
    • Template Caching
    • Mail
    • Uploading
    • View Composers, Sharing, Filters and Tests
    • Caching
    • Broadcasting
    • Queues and Jobs
    • Compiling Assets
    • Framework Hooks
    • Task Scheduling
    • Environments
    • Events
  • Security
    • Authentication
    • Encryption
    • CSRF Protection
  • Orator ORM
    • Basic Usage
    • Query Builder
    • ORM
    • Pagination
    • Schema Builder
    • Database Migrations
    • Collections
  • Managers and Drivers
    • About Managers
    • About Drivers
    • Contracts
  • Official Packages
    • Masonite Entry
    • Masonite Billing
    • Masonite Dashboard
    • Masonite Notifications
  • Tutorials
    • Creating a Blog
  • How-to Guides
    • How To Deploy Masonite to PythonAnywhere
    • How To Use The Repository Pattern with Masonite
    • Deploying a Masonite Application to Heroku
    • Build Email Verification from Scratch With Masonite Framework and JSON Web Tokens
    • Making Masonite and Laravel Mix work together
    • How-To: Use RabbitMQ with Masonite 2.0 queues
  • Deployment
    • Optimization
    • Drivers
Powered by GitBook
On this page
  • Introduction
  • Getting Started
  • Adding Our Command To Craft
  • Adding The Service Provider
Edit on Git
Export as PDF
  1. The Craft Command

Creating Commands

PreviousIntroductionNextRequest Lifecycle

Last updated 6 years ago

Introduction

It's extremely simple to add commands to Masonite via the craft command tool and Service Providers. If you have been using Masonite for any amount of time you will learn that commands are a huge part of developing web applications with Masonite. We have made it extremely easy to create these commands and add them to craft to build really fast personal commands that you might use often.

Masonite uses the Cleo package for creating and consuming commands so for more extensive documentation on how to utilize commands themselves, how to get arguments and options, and how to print colorful text to the command line.

Read more about Cleo by visiting the .

Getting Started

You can create commands by using craft itself:

terminal
$ craft command HelloCommand

This will create a app/commands/HelloCommand.py file with boilerplate code that looks like this:

app/commands/HelloCommand.py
""" A HelloCommand Command """
from cleo import Command


class HelloCommand(Command):
    """
    Description of command

    command:name
        {argument : description}
    """

    def handle(self):
        pass

Let's create a simple hello name application which prints "hello your-name" to the console.

Where it says command:name inside the docstring we can put hello and inside the argument we can put name like so:

app/commands/HelloCommand.py
""" A HelloCommand Command """
from cleo import Command


class HelloCommand(Command):
    """
    Say hello to you

    hello
        {name : Your name}
    """

    def handle(self):
        pass

Inside the handle method we can get the argument passed by specifying self.argument('name'). Simply put:

app/commands/HelloCommand.py
""" A HelloCommand Command """
from cleo import Command


class HelloCommand(Command):
    """
    Say hello to you

    hello
        {name : Your name}
    """

    def handle(self):
        print('Hello {0}'.format(self.argument('name')))

That's it! Now we just have to add it to our craft command.

Adding Our Command To Craft

We can add commands to craft by creating a Service Provider and registering our command into the container. Craft will automatically run all the register methods on all containers and retrieve all the commands.

Let's create a Service Provider:

terminal
$ craft provider HelloProvider

This will create a provider in app/providers/HelloProvider.py that looks like:

app/providers/HelloProvider.py
''' A HelloProvider Service Provider '''
from masonite.provider import ServiceProvider


class HelloProvider(ServiceProvider):

    def register(self):
        pass

    def boot(self):
        pass

Let's import our command and register it into the container. Also because we are only registering things into the container, we can set wsgi = False so it is not ran on every request and only before the server starts:

app/providers/HelloProvider.py
''' A HelloProvider Service Provider '''
from masonite.provider import ServiceProvider
from app.commands.HelloCommand import HelloCommand

class HelloProvider(ServiceProvider):

    wsgi = False

    def register(self):
        self.app.bind('HelloCommand', HelloCommand())

    def boot(self):
        pass

Make sure you instantiate the command. Also the command name needs to end in "Command". So binding HelloCommand will work but binding Hello will not. Craft will only pick up commands that end in Command. This is also case sensitive so make sure Command is capitalized.

Adding The Service Provider

Like normal, we need to add our Service Provider to the PROVIDERS list inside our config/providers.py file:

config/application.py
from app.providers.HelloProvider import HelloProvider
...

PROVIDERS = [
...
    # Application Providers
    UserModelProvider,
    MiddlewareProvider,

    # New Hello Provider
    HelloProvider,

]

That's it! Now if we run:

terminal
$ craft

We will see our new hello command:

terminal
  help              Displays help for a command
  hello             Say hello to you
  install           Installs all of Masonite's dependencies

and if we run:

terminal
$ craft hello Joseph

We will see an output of:

terminal
Hello Joseph
Cleo Documentation