DEV Community


Posted on

MojoForum - A Forum in Mojolicious (Part-2)

Welcome back. This article is in continuation of learning the Mojolicious by developing a project from scratch. The name of the Series is MojoForum and, this is the second article of the series. For the previous write-up, visit here.

The project repository on git is MojoForum.

In the Part-1, we accomplished:

  1. Installation of Perl
  2. Installation of MySQL
  3. Installation of Mojolicious
  4. Create and Launch the Application
  5. Create the Database Tables and their relationships.

We have the database and the tables. However, we do not have the data in the columns. Let's add some data in the database.

Visit the git repository and go to the src_data and download all the CSV files.

Enter fullscreen mode Exit fullscreen mode

The above files are generated by ''. I need to modify the script to generate and insert the data into the MySQL. So that we have the painless data generation process. I think this is where the Mojolicious lags a bit from other frameworks like Django/Laravel where the fake data generation is integrated within the framework. If Mojolicious already has this functionality, Please do let me know.

But it does other things like async and IOLoop much better than the other frameworks so we can live with manual fake data generation. Probably by the end of this series, we'll have the way to generate the fake data directly in to the MySQL

Alternatively, you can clone the git repository, using:

git clone -b YOUR_BRANCH_NAME
Enter fullscreen mode Exit fullscreen mode

NOTE: Please don't clone the master. Create a new branch and then clone it.

If you are using SQL Pro or MySQL Workbench, then import the CSV file using the "Table Data Import Wizard" in the following order:

  1. User.csv
  2. Thread.csv
  3. Post.csv

You can also use command line to import the CSV.

What we have done so far:

  1. We generate fake data for the user, thread, and replies tables.
  2. We import the data into MySQL.

The next step is to quickly go through the files structure generated by the Mojolicous framework.

Package is the entry point of the application. All the routes and basic configuration defined in this Perl package. And then the request is transferred to the controller under the lib/mojoForum folder. From the controller, the information carried to the templates, and then the user views it on the browsers. Please refer the image for more details.

We'll understand the more concepts as we get along with forum development.

Setting up Mojolicious

In this section of the course, we'll set up the Mojolicious with initial configurations required for the development of MojoForum. We set up the database in the earlier section. However, we don't have any method to pull it up from the database. Let's set up the database connection.

Setup DBIx with Mojolicious

For this course, we use DBIx to fetch the data from the MySQL/MariaDB database.

Create a new file dbschema.conf and update the file with the following content

schema_class mojoForum::Schema

# connection string
    dsn     dbi:mysql:MojoForum
    user    your_db_user
    pass    your_db_password

# dbic loader options
    dump_directory ./lib/
    components     InflateColumn::DateTime
    components     TimeStamp
    components     EncodedColumn
Enter fullscreen mode Exit fullscreen mode
dbicdump dbschema.conf
Enter fullscreen mode Exit fullscreen mode

Go to the root of the application directory and run the above command. Please install dbicdump if not installed already.

Meanwhile, the above commands execute successfully. It-dumps the database structure under the Schema folder. And Schema folder is under the lib/mojoForum directory.

Dumping the schema is one of the essential steps in our forum. Yet there is one more steps i.e. link the database with our application.

Create the file under the lib/mojoForum directory.

cd lib/mojoForum
Enter fullscreen mode Exit fullscreen mode

Open the file and add the following content:

package mojoForum::Model::DB;

use mojoForum::Schema;
use DBIx::Class ();

use strict;

my ($schema_class, $connect_info);

    $schema_class = 'mojoForum::Schema';
    $connect_info = {
        dsn      => 'dbi:mysql:mojoForum',
        user     => 'root',
        password => '',

sub new {
    return __PACKAGE__->config( $schema_class, $connect_info );

sub config {
    my $class = shift;

    my $self = {
        schema       => shift,
        connect_info => shift,

    my $dbh = $self->{schema}->connect(

    return $dbh;

Enter fullscreen mode Exit fullscreen mode

Add use mojoForum::Model::DB to just before the startup subroutine. Also add _db_handler() subroutine assign the database handler to our application.

package mojoForum;
use Mojo::Base 'Mojolicious';

use mojoForum::Model::DB;           # Add here

# This method will run once at server start
sub startup {
  my $self = shift;

 # Add Database Handler
 sub _db_handler {
    my $self = shift;

    $self->{ dbh } = mojoForum::Model::DB->new();

    return $self;;

Enter fullscreen mode Exit fullscreen mode

In the main application file, by default there is only route added to the application. We want our home page to load all the forums.

Enter fullscreen mode Exit fullscreen mode

The above is the default route present in the main file. We'll go through it later. For now, replace the line with

$r->get('/')->to(controller => 'Thread', action => 'show');
Enter fullscreen mode Exit fullscreen mode

It states, whenever we have a request for '/' (the root of the application), go to the Thread controller and look out for the show subroutine in the Thread controller package.

Before creating the thread controller, please delete the example folder under the templates and under the controller.

We have not created the Thread controller yet. Please create, under the Controller folder and write the following code.

package mojoForum::Controller::Thread;
use Mojo::Base 'Mojolicious::Controller';

# This action will render a template
sub show {
    my $self = shift;

    my $dbh = $self->app->{dbh};            # Database handler

    # Fetch all the threads from the thread table;
    my @threads = $dbh->resultset('Thread')->search({});

    @threads = map { { 
        title => $_->title,
        body  => $_->body,
        user  => $_->user->first_name. ' ' . $_->user->last_name
    } } @threads;

        template => 'thread',               # Template name, thread.html.ep under the templates folder.
        threads  => \@threads               # Pass thread array ref to the template

Enter fullscreen mode Exit fullscreen mode

Above, we retrieved all the threads from the thread table. And store it in an array. We want to render all the data in the template thread. We also pass the reference of array threads to the template threads => \@threads.

% foreach my $thread (@$threads) {
    <li> <%= $thread->{title} %> </li>
Enter fullscreen mode Exit fullscreen mode

Refresh the webpage.

All the title of the threads listed in the page.

It doesn't looks nice. We'll add it some styles to it.

Adding Style to our Forum

We are going to add some styles in mojoForum to improve the UI look and feel. For our forums, we are going to use CSS only framework and if required, then we add our javascript of our own.

For CSS only framework, we use Tailwind CSS. It is a utility-first CSS framework for quickly building the designs. At present, we directly include the CSS using the CDN, however in the later parts, we limit the size of Tailwind to include the relevant classes to make our application lighter and faster.

Open default.html.ep and update the content with:

<!DOCTYPE html>

    <title><%= title %></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link href="^1.0/dist/tailwind.min.css" rel="stylesheet">


    <div class="container mx-auto">

        <nav class="flex justify-center p-4"><a class="text-xl text-indigo-500 font-semibold" href="#"></a></nav>

        <nav class="flex flex-wrap bg-indigo-500 items-center justify-between p-4">

            <div class="flex flex-shrink-0 mr-10">
                <a class="text-xl text-white font-bold" href="/"> MojoForum </a>

            <div class="block lg:hidden">

                    class="navbar-burger flex items-center py-2 px-3 text-indigo-500 rounded border border-indigo-500">

                    <svg class="fill-current h-3 w-3" viewBox="0 0 20 20" xmlns="">
                        <path d="M0 3h20v2H0V3zm0 6h20v2H0V9zm0 6h20v2H0v-2z" />



            <div class="navbar-menu hidden lg:flex lg:flex-grow lg:items-center w-full lg:w-auto">

                <div class="lg:mr-auto">

                    <a class="block lg:inline-block mt-4 lg:mt-0 mr-10 text-white font-bold hover:text-blue-700" href="#">Threads </a>



                    <a class="block lg:inline-block mt-4 lg:mt-0 lg:mr-8 text-white hover:text-red-500 " href="#">Sign in </a>

                    <a class="inline-block py-3 px-6 mt-4 lg:mt-0 leading-none text-indigo-500 bg-white hover:bg-indigo-600 hover:text-white rounded shadow"
                        href="#">Sign up




        <%= content %>



Enter fullscreen mode Exit fullscreen mode

Refresh the webpage now. :)

We observe that our forum is getting into the shape. Still the list of threads is not looking good. We need to modify it.

Go to the thread.html.ep and modify the content as:

% layout 'default';
% title 'MojoForum - A Forum with Mojolicious';

% foreach my $thread (@$threads) {
    <div class="w-full border rounded py-3 mt-5">  

        <div class="font-bold text-blue-700 ml-5">
            <%= $thread->{title} %> 


Enter fullscreen mode Exit fullscreen mode

Refresh the web page now. We can see that threads are looking much nicer than before.

Still our forums are far from over. We noticed the following problem:

  1. Threads are not clickable.
  2. We cannot see the comments on the thread.
  3. We cannot see at what time it was posted.
  4. User who posted the threads/comments.

We address the above issues in our next article.

All the code pushed to git repository MojoForum.

'Have fun learning Mojolicious'

Top comments (3)

pots07 profile image

Thanks for the articles. Could you include which PERL modules are requires for this project? Perhaps in cpanfile in the repo

akuks profile image

Sorry, I dont have the comprehensive list. But I don't think other than DBI and DBIX you need to install any other module.

alphacoorg profile image
Rakshith Chengappa

Keep it coming!! Awaiting Part 3!