web(cslai)

Findings and Notes in Web Development

MVC : Me vs. CodeIgniter

My Implementation

Based on the previously mentioned definition, I implemented the architecture from scratch for my final year project. However, I also modified the MVC architecture so that the structure also resembles three-tiers architecture in some way. The reason is that I am more familiar with three-tier architecture as I studied that in my college days and found some similarities in between these two architectures.

Therefore the product of my implementation becomes (the codes are being re-written at the moment but basically will be the same as the one posted here and they are for illustration purpose only and may not work)

Model

Basically a model is used to store business logic (methods/functions) and data (attributes/characteristics/field) but I don’t want the logic and data got all mixed together and make the model class file extremely complex and long. Hence, I took out the data and place them in the entity. Therefore a typical model class would look something like the following:


<?php
class TypicalModel extends Model
{
/*
* $dao = data access object
*/
public function __construct($dao)
{
parent::__construct($dao);
}
 
public function TypicalMethod()
{
// some method
}
 
public function AddRecord()
{
// some code to add record to database through the data access object
}
 
public function DelRecord()
{
// some code to delete record from database
}
 
// there maybe some other methods but I think you have got the idea already
}
?>

Entity

The entity class, as mentioned above, is just a class storing the attribute. I am still working on this part to simplify the declaration of the mutators and accessors.


<?php
class TypicalEntity
{
public $var1;
 
public function __construct($var1)
{
$this->SetVar1($var1);
}
 
private function SetVar1($var1)
{
$this->var1 = $var1;
}
 
public function GetVar1()
{
return $this->var1;
}
}
?>

View

The view is used to display output to the user by pulling information from the model. But to pull information from the model, I need to call methods from the view class, Therefore I separate the method declarations and the actual output codes into a view class and a template file. The only problem with this implementation so far is the speed decrease as message passing through method calling is slow.


<?php
class TypicalView extends View
{
public function __construct($model)
{
parent::__construct($model);
}
 
public function Display()
{
include 'TypicalTemplate.tpl';
}
 
public function DisplayTypicalParagraph()
{
return '<p>This is a typical sentence in a paragraph tag.</p>';
}
}
?>

Templates

Although the template can be in any format, I use a simple html page for demonstration purpose.


<html>
<head>
<title>A Typical Page</title>
</head>
<body>
<?php $this->DisplayTypicalParagraph(); ?>
</body>
</html>

Controller


<?php
class TypicalController extends Controller
{
public function __construct($dao)
{
parent::__construct($dao);
}
 
public function Execute()
{
/*
* because I am taught with ASP.NET initially,
* so you may see me working it somehow in
* ASP.NET way
*/
parent::SetModel(new TypicalModel($dao));
$this->model->TypicalMethod();
 
if(count($this->model->errorMsg) == 0)
{
// IsPostBack in PHP
if(count($_POST) == 0)
{
parent::SetView(new TypicalView($this->model));
$this->view->Display();
}
}
}
}
?>

Front Controller

Because I am going to build a site with many modules and I wish users to visit my site through the only entrance which is the index.php. Therefore I use a front controller to redirect my users to the respective module at the specific controller.


<?php
class FrontController extends Controller
{
public function __construct($dao)
{
parent::__construct($dao);
}
 
public function Redirect()
{
switch($_POST['request'])
{
case ‘module/action/’:
SetController(new TypicalController($dao));
break;
}
}
}
?>

Finally, the index.php file

To make the website work, one would only add a few lines into the index.php file to make the site work. One advantage with using my implementation is that I can put only the index.php and .htaccess file in the public_html folder and the above files are stored out of the public_html folder to prevent other people from accessing them regardless of their intention.


<?php
// basically, one line should do
FrontController::Redirect($dao);
?>

Other Notes

Of course the above part does not really work but just a demonstration of my implementation for MVC architecture. I purposely omitted the inclusion of files, validation, passing of http post and get information etc. because they are not relevant. Next, I will touch briefly on CI’s implementation for MVC architecture.

One Comment

[...] comparing my own implementation of MVC with CodeIgniter’s, now I’m comparing Kohana’s and Zend’s. I have just shifted from CodeIgniter to [...]

Leave Message

Personal Details

Toggle Extra

© 如无特别著名,本站的所有著作权归作者Jeffrey04以署名-非商业性使用-相同方式共享许可证发布。

本网志是由Wordpress荣誉呈现,站点界面SimKite设计归设计者Jeffrey04所有。

由于本站采用即时刊登留言者的留言,所以无法也不能完全监察所有的留言,站长Jeffrey04保留删除和不删除留言之权力。欢迎网友发表具建设性,负责任兼且合法之言论,否则已经发现立即删除而不另行通知。倘若网友发现留言出现问题请留言提出,本站保留一切法律权力。