February 12, 2015 8:36 pm

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

We are setting CakePHP’s components which we will use (Session, Auth) and specifying that when a user logs in he has to be redirected to the index() method of the OffersController and when he logs out he has to be redirected to the same place. It is also important that we specify the hashing mechanism which we use (simplepasswordhasher with sha256) because if we do not specify it here is what will happen.

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

When a user registers his password will be hashed with sha256 but when a user tries log in (we specified the hashing in beforeSave so we are not saving anything) the password will automatically be hashed with the default hash type which is a different one ) the passwords will never match.

Then in the beforeFilter() which gets executed no matter in which controller we are unless we override it we specify the variables that all of our views will be using and allow some pages.

$this->Auth->allow takes an array of actions (or controller methods) which a person can enter/access. If we allow something when the user is logged in but do not allow it by default – a user that is not logged in would not be able to enter these pages.

Alternatively, we can use $this->Auth->deny(array(‘action’)) to block a user from accessing certain pages.

We check if a user is logged in with $this->Auth->loggedIn(). Notice that it does not take any arguments. We can access columns in our users table that belong to the logged in user with $this->Auth->user(“columName”);

For example, if we have (and we have) a full_name column that each user has we can access it with $this->Auth->user(“full_name”);

Demo and Code for Download.

Now, we can add our homepage view which is nothing special. We add a Foundation content slider and use the variables we have set in AppController to display a couple of lines.

Notice that we put our View’s content in the page_content entry point.

Now, let’s start working on the OffersController.

We are using the main layout and allowing an admin panel.

In our offers index we want to show the most recent offers.

That should be familiar to you by now. We are setting the most recent offers as a variable that would be shown by the View and fetching it using find(“all”) and passing some options to it so the offers can be ordered starting from the most recent one to the most oldest one and allowing only 25 offers to be shown.

We add the beforeFilter() of the OffersController which calls the AppController’s beforeFilter and shows a different layout if the user is accessing the admin panel (the CakePHP’s standard layout).

Also, we are allowing users to buy a product or add it only if they are logged in.

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: