How To Create A Simple Application Using CakePHP

2

 A sample application using CakePHP

Before checking out this one go through

http://www.durofy.com/computing/introduction-to-cakephp/

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.

d1

Let’s get started with the coding part.

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

t2b

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

<?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;
		 $nam=$this->request->data['username'];
		 $gen=$this->request->data['gender_of_teacher'];
		 $qual=$this->request->data['qualification_of_teacher'];
		 $sub=$this->request->data['subject_you_teach'];
		 $pass=$this->request->data['password'];
		 $savedData=array();
		 $savedData['Teacher']=array( 'username'=>$nam,
                               		 'dgen'=>$gen,
									  'dqual'=>$qual,
									  'dsub'=>$sub,
									  'password'=>$pass		 
									);
			if($this->Teacher->save($savedData))	
					{ //echo "/ndata is saved in the database";
					 $this->Session->setFlash("data is now saved in database, you can now login to your account");
					 $this->redirect(array('action'=>'login'));
					}
			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";

		if(!empty($this->request->data)){
			$nam1=$this->request->data['username'];
			$pass1=$this->request->data['password'];
			$searchData=$this->Teacher->find('first',array('conditions'=>array('username'=>$nam1, 'password'=>$pass1)));
		//echo '<pre>';
		//pr($searchData);exit;
			if(empty($searchData))
			{
				//$message = 'Not Found';
				$this->Session->setFlash("could not find data matching ur login details");
			}
			else
			{
				//$message = 'Login Successfully';
				$this->Session->write('uname',$nam1);
				$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

<?php
class Teacher extends AppModel
	{ 

	}

?>

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

index.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="">
</form>

 login.ctp

<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">
</form>

welcome.ctp

<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

t2g

Now Check the database

t2l

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

t2j

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

t2k

 

Share.

About Author

2 Comments

  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.

Reply To AV Ram Cancel Reply

13 − 7 =

*