This is the part 1 of 4 in Build a forum with CakePHP.
CakePHP official site provides a simple yet powerful comprehensive tutorial about building a blog using CakePHP. However a lot of developers are wondering how to build a forum with CakePHP.
In this series of tutorials, we are going to build a very simple forum with features like list topics, view topics as well as posting replies to topics. It is not a production ready forum. Its purpose is to demonstrate how easy to build a forum with CakePHP, with its built-in features such as authentication, MVC structure and rich view helpers.
In this tutorial we will setup CakePHP properly and use Twitter's Bootstrap for the design.
For simplicity, we are going to call the forum application as "CapForum". Let us get started.
Download link of entire source code is provided in part 4 of this series.
Table Of Content
- Create database
- Install Bootstrap and DebugKit
- Edit layouts and create navigation element
- The end
Download Bootstrap framework from this URL.
We are using version 3.0.2 by the time of writing this tutorial.
- Download CakePHP from this URL. We are using CakePHP 2.4.3 for this tutorial.
- Download DebugKit from this URL. It is a good habit to use DebugKit for CakePHP development. It logs information we need in a nice UI bar.
- Create a database for the forum. In our case, we have created a database called "capforum", this will be the placeholder for our forum's db.
2. Create database
Let us design a database structure for CapForum. We need a "users" table to store users' login credentials, a "forums" table to store forum's information, a "topics" table to store topics across forums, a "posts" table to store replies to each topic. And their relationship is identified as well:
Import the tables to your database using the script below. It will as well add some dummy data since we are not going to build a backend system in this series.
Note that the script above will create an user account with the username "caker" and password "password". Keep them in mind, because we will need them later.
3. Install Bootstrap and DebugKit
- Copy "fonts" folder from downloaded Bootstrap files to CakePHP's directory "app/webroot".
- Copy "bootstrap.min.css" file from downloaded Bootstrap files to CakePHP's directory "app/webroot/css".
- Copy "bootstrap.min.js" file from downloaded Bootstrap files to CakePHP's directory "app/webroot/js".
Now CapForum "app/webroot" directory should look similar as below:
The highlighted parts are what we have added.
Lastly, follow this link to install DebugKit.
4. Edit layouts and create navigation element
Now we have Bootstrap resources in CakePHP directory. We need to edit the layout files to include them in the view.
Copy following files to "app/View/Layouts/default.ctp":
In the layout file above, we used an element "navigation". It is a good idea to keep navigation in an element, so later when you
extend this application, you can easily add the navigation to other layout files.
Copy the following to file "app/View/Elements/navigation.ctp":
You probably have not edit CakePHP database connection file yet. Copy "app/Config/database.php.default" to "app/Config/database.php", and supply correct database access information. You should be familiar with this step; this is an essential step on installing CakePHP.
Before we end this tutorial, there are two important config values you will have to change. Open file "app/Config/core.php":
- Change "Security.salt" to "123G93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi": Configure::write('Security.salt', '123G93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');
- Change "Security.cipherSeed" to "12359309657453542496749683645": Configure::write('Security.cipherSeed', '12359309657453542496749683645');
The reason we are doing this is that Auth component uses these values to match passwords.
5. The end
Now we have completed the first tutorial. Navigate to CapForum index page, you should be able to see the page as below:
This page simply tells us that CakePHP has been installed properly and as you can see it is using Bootstrap's CSS properties.
If your page does not look like above, that means you do not follow some parts of the tutorials correctly, please review and try them again.
Next tutorial, we are going to build authentication system for CapForum.
Hopefully this simple tutorial helped you with your development.
If you like our post, please follow us on Twitter and help spread the word. We need your support to continue.
If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.
You might also want to check out course Up and Running with CakePHP from lynda.com.