Click to change color scheme

Notes on codes, projects and everything

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 lilly deutschland cialis. 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));

}
}
}
?>

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.

leave your comment

name is required

email is required

have a blog?

This blog uses scripts to assist and automate comment moderation, and the author of this blog post does not hold responsibility in the content of posted comments. Please note that activities such as flaming, ungrounded accusations as well as spamming will not be entertained.

Pings