How To Create A Simple Application Using CakePHP


 A sample application using CakePHP

Before checking out this one go through

where topics like Need of CakePHP framework, Installation, Configuring your PhpMyAdmin database CakePHP and Naming Conventions that CakePHP follows are covered.

Now we shall implement those through a simple application.

Sample application

Let’s create an application using CakePHP which lets you enter teacher’s details on an html page and later we can check if the data has been entered in the database or not. This app can be extended to create a login application later. Let’s first draw a raw structure of what we need to do.


Let’s get started with the coding part.

Step I- create the table ‘teachers’ in the database


Step II- create the controller. This file is placed at app/controller/TeachersController.php

class TeachersController extends AppController
	public $helpers=array('Html','Form','Session');
	public $components=array('Session');

	public function index()
		echo "inside fun index inside controller teahers, this function adds data to the database taking values from the registration form\n";
		 if( !empty($this->request->data ))
			{ //pr($this->request->data);exit;
		 $savedData['Teacher']=array( 'username'=>$nam,
					{ //echo "/ndata is saved in the database";
					 $this->Session->setFlash("data is now saved in database, you can now login to your account");
			else  	{ $this->Session->setFlash("Unable to save data");

	public function login()
		echo "inside func login inside controller teachers, this func would check name an password that the teacher has filled and match it against database records";

			$searchData=$this->Teacher->find('first',array('conditions'=>array('username'=>$nam1, 'password'=>$pass1)));
		//echo '<pre>';
				//$message = 'Not Found';
				$this->Session->setFlash("could not find data matching ur login details");
				//$message = 'Login Successfully';
				$this->Session->setFlash("congrats, you have successfully logged in");
				$this->redirect(array('controller'=> 'teachers', 'action'=>'welcome'));

	  public function welcome()
	    {	echo "inside the welcome func inside teachers controller";
			echo $this->Session->read('uname');
		// $this->Session->setFlash("welcome to your account" .$nam1);

Step III- create the model, here we are using only inbuilt functions thus no need to write any function inside this model. This file is placed at app/Model/Teacher.php

class Teacher extends AppModel



Step IV- create the view index.ctp, login.ctp and welcome.ctp


<h3>Registration Form</h3>
<form action="<?php echo $this->html->url(array('controller'=>'Teachers', 'action'=>'index')); ?>" method="POST">
Username:<input type="text" name="username" size="30">
Qualification:<input type="text" name="qualification_of_teacher" size="30">
Gender:<input type="text" name="gender_of_teacher" size="10">
Subject you teach:<input type="text" name="subject_you_teach" size="30">
Password:<input type="password" name="password" size="30">
<input type="submit" name="submit_the_form" size="">


<h3>Login Form</h3>
<form action="login" method="POST">
Username:<input type="text" name="username" size="30">
Password of the teacher:<input type="password" name="password" size="30">
<input type="submit" name="submit_login_details" size="10">


<h1> welcome </h1><br>
 <?php //$nam2=$this->request->data['nam1'];
//echo $abc;
 //print $this->Session->read('nam1');
  echo $this->Session->read('uname');

Step V- Run the application and get this


Now Check the database


Now login using the correct password, you can also try filling the wrong details to get the error message


once you fill the correct entries you will get a welcome page like this.





  1. This content is informative, interesting, engaging and easy to read. I am glad I ran across this information and got to read it. Thank you.

Leave A Reply