Good morning ny dears!!!!!!
Today I will expalin how to understand a simple MVC application using arrays.
It is very valuable lesson indeed.
First we need a model,view and a controller to do this.
Step 01 :
Create a new file called Learn.php and save it in C:\xampp\htdocs\learncake\app\Model
This is the code you need..
<?php
class Learn extends AppModel{
public function give(){
$data =array('name'=>'Ujitha','age'=>'24','color'=>'Sky blue' ); //create the array.
return $data; //return the array.
}
}
?>
Now as I mentioned in the previous lessons create the table called Learn, inside the database.
Step 02 :
Now we need the controller
Here is the code for controller and save it as LearnsController.php in C:\xampp\htdocs\learncake\app\Controller directory.
<?php
class LearnsController extends AppController{
public function show(){
//$this->set('x',"I love CakePHP");
$this->loadModel('Learn'); //load the model Learn
$data=$this->Learn->give(); //call the function inside the model
$this->set('x',$data); // pass the data to view as an array.
}
}
?>
Step 03: This is the last one and create the view.
Here is the code and save it as show.ctp in C:\xampp\htdocs\learncake\app\View\Learns directory.
Here you have to create a folder called Learns in C:\xampp\htdocs\learncake\app\View\ directory and save the show.ctp inside it.
?php
foreach($x as $info){
echo $info;
}
?>
Now it is ok.. Go to the web brooser and type http://localhost/learncake/Learns/show you will see the out put as follows.