February 8, 2015 8:43 pm

How to Build a Marketplace with CakePHP 2 & Foundation 1/6

Hi guys, in these series we are going to create a simple marketplace with CakePHP and Foundation. When a person enters the site, he can only register/login or browse through the offers (he cannot purchase them). When he registers or/and logins, he can browse offers, buy them and add new ones. If the user is admin, he could modify offers and manage orders and users using CakePHP’s scaffolding.

How to Build a marketplace with CakePHP 2 & Foundation 1-6

Our aim is to turn you from knowing nothing about CakePHP to being able to create your own MVC applications using CakePHP.

Here are some photos of how the end website will look like. Of course, you can enhance it visually, fix design issues, add new features or patch security holes and make it a complete production app.

Demo and Code for Download.

How to Build a marketplace with CakePHP 2 & Foundation 1

Figure 1 shows the homepage of the website when a person is not logged in. He can either login,register or browse offers.

How to Build a marketplace with CakePHP 2 & Foundation 2

Figure 2 reveals the registration page where some of the user input does not meet the validation criteria.

How to Build a marketplace with CakePHP 2 & Foundation 3

Figure 3 shows the login page.

How to Build a marketplace with CakePHP 2 & Foundation 4

Figure 4 reveals the navigation when a user has just been logged in as an admin. He can manage the database tables. It also shows the index page of the offers controller which shows the recent offers from all categories.

How to Build a marketplace with CakePHP 2 & Foundation 5

Figure 5 shows the ability to add new offers when a user is logged in. He provides the necessary data and uploads an image of the product.

How to Build a marketplace with CakePHP 2 & Foundation 6

Figure 6 shows a part of the admin panel. You can see all purchased products. When you click on the particular offer – you would get all the data about the particular product – its price, quantity etc. When you click on the user id – you would see all the data about the user and see his address so you could send that particular product to that particular address.

How to Build a marketplace with CakePHP 2 & Foundation 7

Figure 7 reveals a part of the Offers admin panel.

How to Build a marketplace with CakePHP 2 & Foundation 8

Figure 8 reveals the site where products in a particular category has been selected.

How to Build a marketplace with CakePHP 2 & Foundation 9

Installation:

CakePHP does not need installation. You have to download it from http://cakephp.org/ and unzip it in the folder where you want your project to be. We downloaded all Foundation files and added them to their relevant folder – js/css. You can get it from: http://foundation.zurb.com/

Setting up the database tables

We need only one database – we have named it themarket.

The first table we must add is the one with the categories of products that we will have.

CREATE TABLE IF NOT EXISTS  `categories` (
 `id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
 `name` VARCHAR( 255 ) NOT NULL ,
PRIMARY KEY (  `id` )
) ENGINE = INNODB DEFAULT CHARSET = latin1 AUTO_INCREMENT =1;

It only has an id and a name with no foreign keys.

You can add some categories right now if you want:

INSERT INTO `categories` (`id`, `name`) VALUES
(3, 'Laptops'),
(4, 'Tablets'),
(5, 'Desktop Setups'),
(6, 'Smartphones'),
(7, 'Apple'),
(8, 'Hardware'),
(9, 'Periphery');

The second table is the one with offers (it contains user postings of products they wish to sell)

CREATE TABLE IF NOT EXISTS`offers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`category_id` int(11) NOT NULL,
`price` float NOT NULL,
`qty` int(11) NOT NULL DEFAULT '1',
`description` text NOT NULL,
`img` varchar(255) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

In CakePHP, you name your foreign keys with the name of the foreign table followed by an underscore _ and id. You can use a different format but if you follow the conventions your models will work with minimal code.
Thus, category_id and user_id are foreign keys for the table called categories and the table called users. You name your tables starting with lowercase letters and ending in a plural form. Whereas your models are named according to the table name but ending in singular, as you will see in a bit.

Also, created and modified fields must be DATETIME and be empty because CakePHP will be automatically adding relevant dates to them automatically. Created will be filled out when you create a row and the modified column will be updated both when you create the row and when you make changes to it.

The third table is the users table.

CREATE TABLE IF NOT EXISTS `users` (
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`full_name` varchar(255) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`role` varchar(255) NOT NULL DEFAULT 'user',
`address` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

It does not contain any foreign keys but only data about the user. We have used VARCHAR(255) for all columns for simplicity sake.

The last table is the orders; that is where orders will be stored for further processing when someone buys something.

CREATE TABLE IF NOT EXISTS `orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`offer_id` int(11) NOT NULL,
`created` datetime NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

It has foreign keys to the users table and the offers table. Thus, when someone orders something we can see what he ordered and who ordered it (so we can send it to his address).

Part 1: How to Build a Marketplace with CakePHP 2 & Foundation 1/6

Part 2: How to Build a Marketplace with CakePHP 2 & Foundation 2/6

Part 3: How to Build a Marketplace with CakePHP 2 & Foundation 3/6

Part 4: How to Build a Marketplace with CakePHP 2 & Foundation 4/6

Part 5: How to Build a Marketplace with CakePHP 2 & Foundation 5/6

Part 6: How to Build a Marketplace with CakePHP 2 & Foundation 6/6

Marketplace with CakePHP 2 & Foundation Demo & Download

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter


Tutorial Categories:

3 responses to “How to Build a Marketplace with CakePHP 2 & Foundation 1/6”

  1. ProgrammingNewbie . says:

    What version of Cake PHP was used here?

  2. ProgrammingNewbie . says:

    Be nice if there’s a configuration tutorial for Cake PHP, each of these frame works are funky on setup!

  3. Edward Osei-Nyarko says:

    This tutorial is not friendly to beginners

Leave a Reply

Your email address will not be published. Required fields are marked *