MojoForum - Project in Mojolicious
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:
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.
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.
-
MySQL 5.0 onwards, must be installed on your system. If not, install it according to your flavored OS:
- Windows Installation Packages.
- Debian/Ubuntu Unix Installation.
- Redhat/CentOS/Fedora Installation.
- macOS Installation.
MySQL fundamental knowledge.
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
In Mojolicious one line is enough to create the Project. Isn’t is easy?
Application Launch
cd mojoForum
morbo script/mojoForum
If you you receive a message
Server available at http://127.0.0.1:3000
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 the administrative password and you will be at MySQL Command prompt.
Create a new database by typing the following command:
CREATE DATABASE MojoForum;
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;
Select the database for successive operations.
Next step is to create Users, Threads and Replies table.
We can understand the above table structure as:
- Users may have threads.
- Users may add replies to threads.
- Replies belongs to thread.
We also need to establish a relationship between:
- Users and Threads table
- Users and Replies table
- 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;
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;
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;
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,
- To install Mojolicious
- Install MySQL
- Create Database
- 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.
Top comments (5)
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.
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.
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.
Yes, you are right. That's our goal to create as much as tutorials for Mojolicious.
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!