DEV Community

Ashutosh
Ashutosh

Posted on

MojoForum - A Forum in Mojolicious (Part-1)

MojoForum - Project in Mojolicious

banner

I believe that the good way to learn any technology is through the Project. Project based learning helps us to learn the critical parameters of the technology and we also know how to use them on the production environment. Therefore I decided to write a series of articles, where I write on how to develop a forum (MojoForum) with the help of the Mojolicous. Project repository is on the Github. I am developing the forum during the series and then check in the code to the Github.

As we all know, Mojolicious is a light-weight web framework written in Perl. It is simple yet remarkably powerful to write complex web applications. It requires a few modules to start developing web projects.

We all know, the forum is a place where the user can request a question and exchange the views with other users. Without further delay, let's jump into the series.

Before starting the Project in Mojolicous, the following are the prerequisites for this tutorial:

  1. Perl version 5.24 or greater installed on your local machine. On Unix or Mac Operating System, Perl is available through the default OS installation packages, if you are using the Windows machine, install Activestate Perl from here or strawberry Perl from here.

  2. Elementary knowledge of Perl (Not Object Oriented) is essential. If you are not accustomed to basic Perl, then you are not ready for this tutorial. I would recommend acquiring fundamental knowledge of Perl first and continue the tutorial.

  3. MySQL 5.0 onwards, must be installed on your system. If not, install it according to your flavored OS:

    1. Windows Installation Packages.
    2. Debian/Ubuntu Unix Installation.
    3. Redhat/CentOS/Fedora Installation.
    4. macOS Installation.
  4. MySQL fundamental knowledge.

  5. Basic knowledge of HTML, CSS, Tailwind CSS framework.

I am using Visual Studio Code with Tabnine plugin for developing the forums.

Installation of Mojolicious

You are over here!!! You fulfilled all the prerequisites to get started with Mojolicious Application. Great!!!

Let's start with the Mojolicious Installation.

I already write an article for Mojolicous Installation. Please use this link to install Mojolicious.

Start Project in Mojolicious

It's reasonably fair to assume that installation is successful. Let's generate the app.

mojo generate app mojoForum
Enter fullscreen mode Exit fullscreen mode

In Mojolicious one line is enough to create the Project. Isn’t is easy?

Application Launch

cd mojoForum
morbo script/mojoForum
Enter fullscreen mode Exit fullscreen mode

If you you receive a message

Server available at http://127.0.0.1:3000
Enter fullscreen mode Exit fullscreen mode

You are good to. Go to the browser and launch the application http://localhost:3000

Database seeding

A Minimum Viable Product specification of a forum is:

A Database to store forum information and columns and in the database create following tables:

  • Threads
  • Replies
  • Users

Once we set up the database, we need routes to view the data on our web application.

Let's start by creating the database in MySQL first.
We want our forum application DB isolated from other applications therefore, we create a new database in MySQL.

If you have phpmyadmin/MySQL Workbench/SQL pro, you can use these tools to set up a database and it's tables. Otherwise, use the Command line terminal.

Sign in to MySQL database from command prompt:

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

Enter the administrative password and you will be at MySQL Command prompt.

Create a new database by typing the following command:

CREATE DATABASE MojoForum;
Enter fullscreen mode Exit fullscreen mode

Anyways, you are not restricted to use this name. You are liable to select any name for your application database.

Select the database for sequential operations.

USE MojoForum;
Enter fullscreen mode Exit fullscreen mode

Select the database for successive operations.

Next step is to create Users, Threads and Replies table.

We can understand the above table structure as:

  1. Users may have threads.
  2. Users may add replies to threads.
  3. Replies belongs to thread.

We also need to establish a relationship between:

  1. Users and Threads table
  2. Users and Replies table
  3. Threads and Replies table

Create Users table.

CREATE TABLE 'users' (
 'id' int(11) unsigned NOT NULL AUTO_INCREMENT,
 'first_name' varchar(25) NOT NULL DEFAULT '',
 'middle_name' varchar(25) DEFAULT NULL,
 'last_name' varchar(25) NOT NULL DEFAULT '',
 'email' varchar(50) DEFAULT NULL,
 'password' varchar(255) DEFAULT NULL,
 PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Enter fullscreen mode Exit fullscreen mode

Create threads table.

CREATE TABLE 'threads' (
  'id' int(11) unsigned NOT NULL AUTO_INCREMENT,
  'user_id' int(11) unsigned DEFAULT NULL,
  'title' varchar(50) DEFAULT NULL,
  'body' mediumtext,
  PRIMARY KEY ('id'),
  KEY 'user_id' ('user_id'),
  CONSTRAINT 'threads_ibfk_1' FOREIGN KEY ('user_id') REFERENCES 'users' ('id') ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Enter fullscreen mode Exit fullscreen mode

Create replies table

CREATE TABLE 'replies' (
  'id' int(11) unsigned NOT NULL AUTO_INCREMENT,
  'user_id' int(11) unsigned DEFAULT NULL,
  'thread_id' int(11) unsigned DEFAULT NULL,
  'body' mediumtext,
  PRIMARY KEY ('id'),
  KEY 'user_id' ('user_id'),
  KEY 'thread_id' ('thread_id'),
  CONSTRAINT 'replies_ibfk_1' FOREIGN KEY ('user_id') REFERENCES 'users' ('id') ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT 'replies_ibfk_2' FOREIGN KEY ('thread_id') REFERENCES 'threads' ('id') ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Enter fullscreen mode Exit fullscreen mode

We have successfully created the database and tables, and columns. We also establish the relationship between users, thread and replies table.

That's all for this article. In this article we learn,

  1. To install Mojolicious
  2. Install MySQL
  3. Create Database
  4. Create users, threads and replies table.

In the next article, we learn to seed the database and setup the scaffolding of our Forums application.

Stay tuned.

Discussion (5)

Collapse
alphacoorg profile image
Rakshith Chengappa

Is there a part - 2 coming ? Big perl fan here. Recently developed this interest of using perl for web development. But I see there are not much users using mojolicious(I could be wrong). What are your thoughts in using perl for web development ? Would love to read rest of the parts of this article.

Collapse
akuks profile image
Ashutosh Author

Part-2 is already released dev.to/akuks/mojoforum-a-forum-in-...
I am working on the Part-3. But due to time constraint it got delayed. It'll out this weekend.

Perl is still used for web development it is not as popular as PHP. Mojolicious/Dancer are in fashion. There is one more framework Catalyst but it's community is shrinking. BTW Mojolicious developed by the same guy who developed Catalyst.

Collapse
alphacoorg profile image
Rakshith Chengappa

Oh Great!! I will go through it right away. Couldn't see it under posts that you have published. Thank you.

I know about SRI who developed both. I have used PHP for web development in the past. Have also used and customized both Joomla and Wordpress. Did some basic programming using Perl/CGI eons ago when I was in college. Just apprehensive to start web development in perl considering it is less popular which means if you are stuck somewhere you won't find answers that easily unlike say Laravel.

Thread Thread
akuks profile image
Ashutosh Author

Yes, you are right. That's our goal to create as much as tutorials for Mojolicious.

Thread Thread
alphacoorg profile image
Rakshith Chengappa

Hi Ashutosh,

Since your tutorials helped me with learning Mojolicious. I have added link to this article from my website.

You can check it out here: thecurioustechnoid.com/mojolicious...

Cheers!

  • RC