Hello dear friends today I am here to tell you about a simple example in CakePhp.
To do this we need MVC.That is a model,view and a controller.
****Please remember to follow the correct naming conventions in CakePhp[Case sholud be correct].
****So here onword I will mention nameing conventions as 'CONVENTION'
1. First we need a model.
Open a new file and save it as Learn.php in C:\xampp\htdocs\learncake\app\Model directory.
Paste below codes in it.
<?php
class Learn extends AppModel {
}
?>
CONVENTION: the class name and the name used to save the file should be the same and case also same[Learn].
CONVENTION: Now it is good to remember to create a table in our database[created in the previous lessons] which has got name similar to the newly created model.[Create a table called Learn]
2.Now we need the controller.
Open a new file and save it as LearnsController.php in C:\xampp\htdocs\learncake\app\Controller directory.
CONVENTION: The controller name sholud be the plural of the model name.[In this case model is Learn so controller name sholud be LearnsController]
Paste below codes in it.
<?php
class LearnsController extends AppController{
public function show(){
$this->set('x',"I love CakePHP"); //assign the String to x variable
}
}
?>
3. Now we need a view file to view the data pass by controller[In our case the data is x].
Create a folder in C:\xampp\htdocs\learncake\app\View as Learns.
CONVENTION: The name of this folder should be same to the name of controller.
4. This is the last step.
Open an empty file and save it in C:\xampp\htdocs\learncake\app\View\Learns directory as show.ctp .
CONVENTION: The function name in the controller and the name of the viwe file should be the same.
save these below cades in it.
<?php
echo $x;
?>
5.That is it.
Go to the web browser and type http://localhost/learncake/Learns/show you will see the below output.
To do this we need MVC.That is a model,view and a controller.
****Please remember to follow the correct naming conventions in CakePhp[Case sholud be correct].
****So here onword I will mention nameing conventions as 'CONVENTION'
1. First we need a model.
Open a new file and save it as Learn.php in C:\xampp\htdocs\learncake\app\Model directory.
Paste below codes in it.
<?php
class Learn extends AppModel {
}
?>
CONVENTION: the class name and the name used to save the file should be the same and case also same[Learn].
CONVENTION: Now it is good to remember to create a table in our database[created in the previous lessons] which has got name similar to the newly created model.[Create a table called Learn]
2.Now we need the controller.
Open a new file and save it as LearnsController.php in C:\xampp\htdocs\learncake\app\Controller directory.
CONVENTION: The controller name sholud be the plural of the model name.[In this case model is Learn so controller name sholud be LearnsController]
Paste below codes in it.
<?php
class LearnsController extends AppController{
public function show(){
$this->set('x',"I love CakePHP"); //assign the String to x variable
}
}
?>
3. Now we need a view file to view the data pass by controller[In our case the data is x].
Create a folder in C:\xampp\htdocs\learncake\app\View as Learns.
CONVENTION: The name of this folder should be same to the name of controller.
4. This is the last step.
Open an empty file and save it in C:\xampp\htdocs\learncake\app\View\Learns directory as show.ctp .
CONVENTION: The function name in the controller and the name of the viwe file should be the same.
save these below cades in it.
<?php
echo $x;
?>
5.That is it.
Go to the web browser and type http://localhost/learncake/Learns/show you will see the below output.
That is it!!!!!Enjoy it and understand what happens here.
-Ujitha Manujapriya Sudasingha-
-Ujitha Manujapriya Sudasingha-