Time for HMVC

You know MVC ? Of course you know. And now meet HMVC

Very simple implementation in php:

1) Structure

2) index.php

include '../lib/view.class.php';

include '../config/config.class.php';
include '../lib/routing.class.php';
echo Routing::renderFromUrl();

3) .htacces file

Options -Indexes
RewriteEngine on

RewriteRule  ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$  index.php?controller=$1&method=$2  [L]

4) controller.class.php for news module

class NewsController
{
	public function executeIndex()
	{
		$data = NewsModel::getInstance()->getSomeData();

		$view = new NewsView();
		$view->setTpl('news/view/tpl/index');
		$view->setData('data1',$data);
		$view->setData('data2','dodatkowe dane');

		return $view->render();
	}
}

5) model.class.php file from news module

class NewsModel
{
	private static $oInstance = false;

	public static function getInstance()
	{
		if (self::$oInstance == false)
		{
			self::$oInstance = new NewsModel();
		}
		return self::$oInstance;
	}

	public function getSomeData()
	{
		$data = array(
			0 => 'Lorem1',
			1 => 'Lorem2',
			2 => 'Lorem3',
			3 => 'Lorem4',
			4 => 'Lorem5',
			5 => 'Lorem6',
			0 => 'Lorem7'
		);
		return $data;
	}
}

6) view.class.php file from news module :)

class NewsView extends View
{
}

7) routing.class.php from lib

class Routing
{
	static function  getPostParam()
	{
	   return isset ( $_POST ) ? $_POST : null ;
	}

	static function  getGetParam()
	{
	   return isset ( $_GET ) ? $_GET : null;
	}

	static function renderFromUrl()
	{
		$_controller   = isset ( $_GET['controller'] ) ? ($_GET['controller']) : 'news' ;
		$_method       = isset ( $_GET['method'] ) ? $_GET['method'] : 'index' ;

		return self::render($_controller,$_method);
	}

	static function render($_controller,$_method,$_param=array())
	{

		include_once '../app/'.$_controller.'/controller/controller.class.php';
		include_once '../app/'.$_controller.'/model/model.class.php';
		include_once '../app/'.$_controller.'/view/view.class.php';

		$nameControler = ucfirst($_controller).'Controller';
		$controller = new $nameControler;
		$content =  call_user_func(array($controller,'execute'.ucfirst($_method)),$_param);
		return $content;
	}

}

8) view.clas.php file from lib

class View
{
	private $tpl = '';

	public function setTpl($_tpl)
	{
		$this->tpl = $_tpl;
	}

	public function setData($_key,$_data)
	{
		$this->$_key = $_data;
	}

	public function render()
	{
		ob_start();
			include '../app/'.$this->tpl.'.tpl.php';
		$contents = ob_get_contents();
		ob_end_clean();
		return $contents;
	}
}

9) inex.tpl.php file from news module

<!--?php var_dump($this--->data1); ?>
<!--?php var_dump($this--->data2); ?>

<!--?php echo Routing::render('widget','top'); ?-->
<!--?php echo Routing::render('widget','index'); ?-->
<!--?php echo Routing::render('widget','bottom'); ?-->

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany.

Możesz użyć następujących tagów oraz atrybutów HTML-a: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>