Hi all,
I am explaining here how we can build large interactive applications in any technology not even in php. We can build a very powerful applications using Three tier architecture known as Model View Controller i.e MVC. There are so many mvc frameworks used in php such as cakephp, codeigniter, zend, laravel, yii etc. As we all know that php is open source, we can also develop our own mvc application.
Model: Model defines the database structure, It deals with the controller and send the response to the controller.
View: View defines the user interface of the application based on MVC platform, when the view is loaded, it interacts with the controller and controller interacts with the model to show the result in the View page.
Controller: Controller is the middle and main part which interacts with the both Model and View, It deals with the Model to get the results and deals with the View to display the result retrieved from the model.
we will build a small Hello World application to show how mvc works:-
- Create a folder with the name model.
- Create a folder with the name view.
- Create a folder with the name controller.
- Create a folder with the name assets.
- Create a folder with the name config.
Inside model folder , we will keep the model files, inside the controller will keep the controller files and in the view folder we will create a folder name and files.
1. Create a connection file, connect.php under config folder and write a piece of code:
$conn = mysql_connect('localhost','root','');
mysql_selectdb('dbhelloworld',$conn);
- Create a Hello.php file inside the model folder and write a piece of code:-
include "config/connect.php";
class Hello{
public $name;
public function display_name(){
$this->name = "Hello World";
return $this->name;
}
}
- Now create a HelloController.php in controller folder and write a piece of code:-
class HelloController{
public function view($filename = ""){
if (!class_exists('Hello')) require "model/Hello.php";
if ($filename == 'index'){
Hello::display_name();
$this->call($filename);
}
else{
$this->call($filename);
}
}
private function call ($filename){
if (file_exists("View/".$filename)){
$filename = $filename.".php";
header('Location: View/$filename');
}
else{
return $this->show_error();
}
}
private function show_error(){
return "File not found!!";
}
}
0 Comment(s)