July 23, 2012

Manage your web app with commands

A less known, but very important feature of Mojolicious is the ability to add your own commands to your web application. These commands can ship with your app, so that your management utilities have direct knowledge of your application.

These are some examples of commands that ship with the framework:

Prints out the routes of your web app
generate makefile
Generates a makefile specifically for your web app
Moves embedded templates into separate template files

There are also commands not tied to a specific app:

generate app
Generates scaffolding for a new web app
Generate scaffolding for a Mojolicious plugin
Upload a plugin to CPAN

Here is the thing to remember:

It's super easy to write your own command

How many times have you wasted time manually adding your preferred toolkits to your web app? How about saving yourself some time by throwing this command together?

$ mojo generate app boilerplate -w bootstrap -w backbone -w font-awesome

Or maybe you have little scripts that manage your database, scripts with hard-coded database connection info? Or maybe you programatically pull config info from a separate file? Why not make your life easier and get the database connection directly from the app?

With Mojolicious commands, you get direct access to your entire web app for free; you can even run a web request against it to get the results of a route, and use that information in your command.

There's no difference between the built-in commands and the commands you write yourself.

And it gets even better. What is the deployment process for your app? Multiple steps? Why not simplify it to one step, and distribute it with your web app?

$ myapp deploy

So much more is possible. For example, the recently released deploy heroku eliminates the need to know anything about the deploy platform, instead handling all the details with sensible defaults.

Mojolicious commands are extremely powerful, and CPAN is waiting - what commands have you hoped for in the past? What can you throw together to make your development that much easier?

Your customized command is only one subroutine away!

screenshot of adding commnds section of mojolicious cookbook

Hey! Follow me on twitter!

comments powered by Disqus