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:

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:

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

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)

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.


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: