(note (code cslai))

MVC : Me vs. CodeIgniter

Background

While my static pages and site theme is still under construction, I went to CodeIgniter.com to see how to work with it and played the tutorial screencasts. The reason behind in considering CodeIgniter (CI) to be used in my next project is because I don’t feel like re-inventing the wheel. However to port my current project to use CI may cause some problems as there are differences in how we implement MVC structure.

Model-View-Controller Architecture

Quoted from Java BluePrints:

Model
The model represents enterprise data and the business rules that govern access to and updates of this data. Often the model serves as a software approximation to a real-world process, so simple real-world modeling techniques apply when defining the model.
View
The view renders the contents of a model. It accesses enterprise data through the model and specifies how that data should be presented. It is the view’s responsibility to maintain consistency in its presentation when the model changes. This can be achieved by using a push model, where the view registers itself with the model for change notifications, or a pull model, where the view is responsible for calling the model when it needs to retrieve the most current data.
Controller
The controller translates interactions with the view into actions to be performed by the model. In a stand-alone GUI client, user interactions could be button clicks or menu selections, whereas in a Web application, they appear as GET and POST HTTP requests. The actions performed by the model include activating business processes or changing the state of the model. Based on the user interactions and the outcome of the model actions, the controller responds by selecting an appropriate view.

Note: You may have to view the following page with the source view as I am still looking for a good syntax highlighting plugin for my wordpress installation.

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.

CodeIgniter’s Implementation

Before I start, I would have to say that I am new to CI and this post is written before I actually start developing using CI. The following code is mainly based on the tutorial screencasts and the official user manual. This post is written just to highlight the differences between our implementation and that’s all.

Because CodeIgniter is a full-featured framework, hence the developer doesn’t have to worry about file inclusion and other stuff as long as he/she develops the site according to some simple guidelines.

Model

Because CI doesn’t care about three-tier architecture, so there are only three parts in their MVC implementation. The first is the Model and a typical model would look something like the following (taken from the manual)


class Model_name extends Model {
 
function Model_name()
{
parent::Model();
}
}

View

The view to display output. I haven’t start digging the code to see how the passing of message is done but it seems that the information comes from the controller? (The code snippet below is taken from the manual)


<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $heading;?></h1>
</body>
</html>

Controller

The url structure of CI is explained here. Therefore basically the first part of the url is the module, followed by the method name and the parameter as below

http://www.id.tld/module_name/method_name/parameter/

And the code which corresponds to the URL (Again, the following code is taken from the manual)


<?php
class Blog extends Controller {
 
function index()
{
echo 'Hello World!';
}
 
function comments()
{
echo 'Look at this!';
}
}
?>

Conclusion

To port my current development project to CI, I would have to either rewrite my MVC architecture or find a way to integrate my MVC implementation to CI’s MVC implementation. I really hope that I do not have to rewrite everything again just because I want to use CI.

Exit mobile version