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.

class AppController extends Controller {
	public $components=array(
	'Session',
	'Auth'=>array(

	'loginRedirect'=>array(
	'controller'=>'offers',
	'action'=>'index',
	'admin'=>false
	),
	'logoutRedirect'=>array(
	'controller'=>'offers',
	'action'=>'index',
	
	),
	'authenticate'=>array(
'Form'=>array(
'passwordHasher'=>array(
'className'=>'Simple',
'hashType'=>'sha256'
                )
             )),

	'authorize'=>array('Controller')
	
	)
	);

	public function isAuthorized() {
		if (empty($this->Auth->user("role")) ||!($this->Auth->user("role") ==="admin")) {
			return false;
		}
		return true;
	}
	public function beforeFilter() {

		$this->set("loggedIn", $this->Auth->loggedIn());
		if ($this->Auth->loggedIn()) {
			$this->set("userFullName", $this->Auth->user("full_name"));
			$userIsAdmin= ($this->Auth->user("role") ==="admin") ?true:false;
			$this->set("userIsAdmin", $userIsAdmin);
			$this->Auth->allow("logout");
		}
		
		$this->Auth->allow(array(
			'register', "login", 'index', 'view'
			));
		$this->loadModel("Category");
		$this->set("categories", $this->Category->find("all"));
	}
}

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.

<?php $this->start("page_content"); ?>
<ul class="example-orbit-content text-center"data-orbit>
<li data-orbit-slide="headline-1">
<div>
<h2>The Market. Your Marketplace</h2>
<h3>Sell and buy anything you want that has to do with IT.</h3>
</div>
</li>
<li data-orbit-slide="headline-2">
<div>
<h2>Many categories to find exactly the technology you want.</h2>
<h3>Laptops, tablets, desktop setups, smartphones, hardware, periphery</h3>
</div>
</li>
<li data-orbit-slide="headline-3">
<div>
<h2>Sell and earn or buy and save!</h2>
<h3>Your choice but remember that you can do both.</h3>
</div>
</li>
</ul>

<div class=" text-center">
	<p><?php echo $totalOffers; ?> offers waiting to be purchased!</p>
	<p><?php echo $totalUsers; ?> users waiting for you to sell them a good!</p>

	<h3>What are you waiting for?</h3>
</div>
<?php $this->end(); ?>

Now, let’s start working on the OffersController.

class OffersController extends AppController {
		public $layout="main";
		var $scaffold="admin";

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

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

public function index() {
	$options=array(
	'order'=>array('Offer.created DESC'),
	'limit'=>25, //int
		);
	$recentOffers=$this->Offer->find("all",$options);
	$this->set("recentOffers", $recentOffers);
}

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.

public function beforeFilter() {

			parent::beforeFilter();
			$isAdmin=!empty($this->request->params['admin']);

			if ($isAdmin) {
				$this->layout="default";
			}

			if ($this->Auth->loggedIn()) {
				$this->Auth->allow("add", 'buy');
			}
			else {

				$this->Auth->deny("add", 'logout', 'buy');
			}
		}

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:

Leave a Reply

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