Build MVC Web Applications in Perl 6.
Switch branches/tags
Nothing to show
Latest commit f35874a Apr 25, 2017 @supernovus committed on GitHub Merge pull request #7 from zoffixznet/patch-1
Use modern META filename

Web::App::MVC Build Status


A set of extensions to Web::App providing a MVC-style framework for building dynamic web applications.

We include a few base classes and roles, for quickly defining Controllers, capable of loading one or more Models (with built-in support for models based on the DB::Model::Easy library) and displaying one or more Views.

Example Application Script

    use Web::App::MVC;
    use My::Controller;

    my $app  =<./conf/app.json>);



Example Configuration Files


      "connector"   : {
        "type"      : "SCGI",
        "port"      : 8118
      "views"       : {
        "type"      : "Template6",        
        "dir"       : "./templates"
      "db"          : "./conf/db.json",
      "models"      : "./conf/models.json"


      "My::Models::Example" : {
        ".include" : "db.defaultdb",
        "table"    : "mytable"


      "defaultdb" : {
        "driver" : "mysql",
        "opts"   : {
          "host"     : "localhost",
          "port"     : 3306,
          "database" : "myappdb",
          "user"     : "myappuser",
          "password" : "myapppass"

Example Controller Library

    use Web::App::MVC::Controller;
    use My::Models::Example;
    class My::Controller is Web::App::MVC::Controller {
      method handle ($context) {
        $context.content-type: 'text/html';
        my $id = $context.get('id', :default(1));

        my $model = self.get-model(My::Models::Example);
        my $user = $model.getUserById($id);
        my $name = $;

        my $jobusers = $model.get.with(:job($user.job)).and.not(:id($;

        $context.send: self.render('default', :$name, :$jobusers);

Example Model Library

    use DB::Model::Easy;
    class My::Models::Example::User is DB::Model::Easy::Row {
      has $.id;
      has $.name is rw;
      has $.age  is rw;
      has $.job  is rw;

      ## Rules for mapping database columns to object attributes.
      ## 'id' is a primary key, auto-generated. The column for 'job' is called 'position'.
      has @.fields = 'id' => {:primary, :auto}, 'name', 'age', 'job' => 'position';
    class My::Models::Example is DB::Model::Easy {
      has $.rowclass = My::Models::Example::User;
      method getUserById ($id) {

Example View Template

        <title>Hello [% name %]</title>
        <h1>Other users with the same job as you</h1>
          [% for jobusers as user %]
            <th>[% %]</th>
            <th>[% user.age %]</th>
          [% end %]

More examples

See the included examples in the 'test' folder for even more examples, including the use of the Web::App::Controller::MethodDispatch role.

Configuration File Directives

The main application configuration file, specifies certain settings that will be used in various places in your application. You can add as many additional settings and config files as you want for your own needs, below is a list of known and/or required settings.


Required by Web::App::MVC, this hash specifies which connector engine to use to handle your application. We support the same connectors as Web::App itself. The type key determines the name of the library to use, and all other options will be passed to the respective libraries. At this time, the only other option supported (and indeed required) by the libraries, is the port option, which determines which port the connector will run on.

The types may be specified as short keys (and are case insensitive):


If you are using the View capabilities provided with the Controller class, this hash directive specifies the template engine and options for it to use to parse your views.

As with connector, the type key specifies the name of the supported template engine library you want to use, and all other parameters are passed to the engine.

We support any template engine that has a wrapper class available in the Web::Template library.

The types may be specified as short keys (And are case insensitive):

All of these engines support a dir option that can be a single path, or an array of paths, which specify the paths to find templates in.


This specifies the file name for the models configuration file.

If you are using the get-model() method of the Controller class, and there is a key in the models configuration that matches the class name of your model, then the hash entries within will be passed as named options to your model class.

There are no standards for this, unless you are using the DB::Model::Easy base class, in which case you must supply a table parameter here. You may also specify driver and opts parameters here, but they are best stored in a separate configuration file, which can be included here using a .include parameter (see below.)


This specifies the file name for the database configuration file.

If you are using the DB::Model::Easy base class, then it needs to know the connection details for databases. This file should exist to contain the configuration details.

You can refer to these database configurations from the model configuration, using a special .include parameter which uses a limited JSON Path syntax.

The driver and opts parameters will determine the db connection.

Supported drivers are whatever DBIish supports. Currently this is:

  • mysql
  • Pg
  • SQLite

The opts parameters differ from one driver to the next. See the DBIish documentation for more details.


Timothy Totten. Catch me on #perl6 as 'supernovus'.


Artistic License 2.0