Do with your own php mvc framework!

Most of us have used a php framework to develop our project, for example, zend framework, cakephp, codeigniter and so on.Yet, they are not smart enough for us to make a small project which we also want to use mvc(Model-View-Control).
Here i will introduce you a new fun way to do it.
I have six files here:index.php,views/someview.php,
application/controller.php,application/load.php,application/model.php,application/tinyMvc.php.
You can find something from the floders and files, as those ‘huge’ framework do, we can use these files to load files automatically and load views by one method.
Here is the most code:
index.php

// Display errors in production mode
ini_set('display_errors', 1);

// let's get started
require 'application/tinyMvc.php';

controller.php

class Controller {
   public $load;
   public $model;

   function __construct()
   {
      $this->load = new Load();
      $this->model = new Model();

      // determine what page you're on
      $this->home();
   }

   function home()
   {
      $data = $this->model->user_info();
      $this->load->view('someview.php', $data);
   }
}

model.php

class Model {
   public function user_info()
   {
      // simulates real data
      return array(
         'first' => 'Jeffrey',
         'last'  => 'Way'
      );
   }
}

load.php

class Load {
   function view( $file_name, $data = null )
   {
      if( is_array($data) ) {
         extract($data);
      }
      include 'views/' . $file_name;
   }
}

tinyMvc.php

require 'load.php';
require 'model.php';

require 'controller.php';
new Controller();

someview.php




   



Hello From the View

That is the code, after the code i want you to know this function extract:

$array = array('name'=>'jack', 'sex'=>'male', 'age'=>38, 'address'=>'America');

var_dump($array).'\n';

extract($array);

var_dump($array).'\n';

Implement the code above you will find it mostly like list,but of much difference. I think of it when i see extract!

Tagged: mvc

1 comment

  1. [...] time, i wrote the do-with-your-own-php-mvc-framework and i found it is very useful.Last week we had a test of making a php guestbook, and i do that with [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

WordPress SEO fine-tune by Meta SEO Pack from Poradnik Webmastera