February 11, 2015 6:28 pm

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

Now its time to configure Database, routes and configure and run your homepage so make sure that you have correct database credentials to put in cakePHP and properly imported database.sql file in your database, so lets start with with 3rd part of the series of 6 articles download code and demo.

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

Rename your database.php.default to database.php (it is located in app/Config/ ) and insert your database credentials in the $default property. Here is how my settings look like:

public $default=array(
		//'encoding' => 'utf8',

Open app/Config/core.php find Security.salt and Security.cipherSeed and edit some of the characters/numbers.

Now open,routes.php in the same folder and change the homepage:

Router::connect('/', array('controller'=>'Other', 'action'=>'display', 'home'));

Here, we are assigning OtherController’sdisplay() method to be called when a person tries to visit the homepage.

Open app/Controller and create OtherController.php


class OtherController extends AppController {
	public $layout="main";

	public function beforeFilter() {
	public function display() {
		$this->set("totalOffers", $this->Offer->find("count"));
		$this->set("totalUsers", $this->User->find("count"));

Here is the code we are using. The $layout property sets the layout that all of your Views will be using (your individual pages will be dependent and extend this particular layout). For example, you need a layout if all of your pages have to have the same menu (which we need in that case).

The display method will be called whenever the home page is accessed. In that case, we are loading our offers table and our users table so we can retrieve data from these tables with CakePHP after which we get a number showing all users and all offers we have in our database.

You can make queries for a particular model or table by using $this->ModelName->method

In this case we are using find(“count”) which gives the total rows we have but we could use find(“all”) which gives not a number but all our rows with all data attached to them along with any associated models (it returns an array)

beforeFilter() is executed before all actions in the particular controller. Here we are executing the beforeFilter() of the parent of OtherController which is AppController (this is our base controller so we should add stuff to it whenever we want some logic to be executed for each controller. For example, we may set a variable that all of our Views could use).

$this->set(variableName, variableValue) gives us access to $variableName in our view (it will contain variableValue)

Now we have to create our main layout which all pages would use in this particular project.

Create app/View/Layouts/main.ctp( views typically have .ctp file extension in CakePHP)

<!DOCTYPE html>
<html lang="en">
	<meta charset="UTF-8">
	<title>Ultimate IT marketplace | The Market </title>

	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
	<link rel="stylesheet"href="/css/foundation.min.css">
	<script src="/js/foundation.min.js"></script>
	<link rel="stylesheet"href="/css/styles.css">

<nav class="top-bar" data-topbar role="navigation">
<ul class="title-area">
<li class="name">
<h1><a class="active" href="/">The Market</a></h1>
<!-- Remove the class "menu-icon" to get rid of menu icon. Take out "Menu" to just have icon alone -->
<li class="toggle-topbar menu-icon"><a href="#"><span>Menu</span></a></li>

<section class="top-bar-section">
<!-- Right Nav Section -->
<ul class="right">

<!-- Left Nav Section -->
<ul class="left">
<?php if (!$loggedIn) : ?>
<li><?php echo $this->Html->link("Login",
	array('controller' => 'users',
		'action' => 'login')
<li><?php echo $this->Html->link('Register',
	array('controller' => 'users',
	'action' => 'register'));?>
	<?php endif; ?>
	<?php if ($loggedIn) : ?>
<li class=><a href="/offers/add">Add Offer</a></li>
	<?php endif; ?>
<li class="has-dropdown">
<a href="#">Offers</a>
<ul class="dropdown">
<?php foreach ($categories as $category)  :?>
echo $this->Html->link($category['Category']['name'],
		'action' =>  'view/' . str_replace(" ", "-",strtolower($category['Category']['name'])),
		'controller' => 'offers'
<?php endforeach; ?>
<?php if (isset($userIsAdmin) && $userIsAdmin) : ?>
	<li><a href="/admin/users">Manage Users</a></li>
	<li><a href="/admin/orders">Manage Orders</a></li>
	<li><a href="/admin/offers">Manage Offers</a></li>

<?php endif; ?>
<?php if ($loggedIn) : ?>
<ul class="right">
		<li  style="margin-right: 25px;color: #fff">Hello, <?php echo $userFullName; ?>
		<?php echo $this->Html->link('Logout',
				'controller' => 'users',
				'action' => 'logout'
				'class' => 'button small alert'
				); ?>

<?php endif; ?>
<div id="content">

<?php echo $this->fetch("page_content"); ?>

<div id='footer'>
	<div style="max-height: 80px;"class="panel text-center columns large-12">
		Copyright &copy; 2015 Ivan Dimov &amp;PHPGang


$(".close").click(function() {

$(".buy").click(function(evt) {
	var product = $(this).parent().siblings(".title").text();
	var bought = confirm("Are you certain that you want to buy "+ product +"?");

	if (bought) {
		return true;
	else {
		return false;



Of interest here is that you see the syntax for using CakePHP’s Html helper for creating links. You specify the name of the link and then as a second argument – an array with the controller and action (controller’s method) that you want your link to point to.


<?php echo $this->fetch("page_content"); ?>

Each page will define page_content and the layout will echo it (display the contents of each page at the particular spot where have this line of code).

If we forget to extend page_content in our View – our content would not be visible (only the layout will appear).

In our particular Views we can specify that we want to insert that content in the layout’s page_content entry point by typing:


Notice also that we are displaying menu items only for particular users. These variables will be defined with $this->set in our AppController so each View and the main layout will have access to the $categories, $loggedIn, $userIsAdmin and the other variables.

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:

Leave a Reply

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