"MVC MS" is a compact suite which can easily be used in any PHP script.
MVC is an Object Oriented (OO) design pattern. A Model-View-Controller uses
classes to organize Business Logic (where data is stored, who is authorized to
manipulate it, and how the data is manipulated) contained in "Models",
Presentation Logic (how the data from the Models should be rendered) in
"Views" and has an overall flow for the application within a "Controller".
Current project is not pure realization of the MVC paradigm, but it based
on the MVC conceptions.
With "MVC MS", developers can build applications with the standard user
authentication complete, and developers can drop-in modules to handle extra
user information and add functionality.
Requirements:
>> Smarty 2.6.0
>> ADODB 4.05
Sections
> 1. Intro
> 2. Model
> 3. View
> 4. Controller
> 5. Setup
> 6. Example
1. Intro
MVC is a solution for a problem that web sites map to very well.
Each page hit is a user interaction (input) with your system (the web server
processing the PHP scripts). Assuming you have some kind of need to
maintain the state of your data between page hits (using a persistent
data store - usually a database or files), and you intend to present
this information in a variety of ways to the user, then the MVC pattern may
be the architecture you want to use for your application. The MVC pattern
addresses three problems in architecting a solution:
- Maintaining your data in some kind of persistent storage
- Maintaining the logic controlling the flow of the application,
what to display to the user, and what actions the user is allowed to perform
on the data in your application.
- Presenting information to users of your application
2. Model
Models are the portion of the MVC application that implement the
"Business Logic". Business logic is any PHP logic relating to how
information is stored in your application.
The name Model comes from that fact that these objects emulate things
from the real world, both rules and data. Models are the only component
of your system that should interact with your persistent data store.
Models can implement any of the technologies that make PHP such a great
language for web development: databases calls, file manipulation,
session management, web services, and anything else that stores data
for your application.
Model conception is presented in current project through the number of
classes for authentification. These classes use session mechanism for
storing user information. ADODB wrapper is used for realize access to
datebase layer.
IUser --+
| CAdmin
| CMember
| CAdmins
| CMembers
3. View
Views are the portion of the MVC application that present output to the user.
The most common output for PHP web applications would be HTML, but views are
not restricted to this. Your MVC application might output XML, WML, plain text,
images, email or some other content. PHP has a variety of capabilities to use
in views, ranging from just being designed to send data back to the browser,
to implementing template solutions, to XSLT.
View conception is presented in current project through the template engine.
We are using for it Smarty engine.
4. Controller
The controller is the heart of the MVC application. This component is the
object that should be aware of the user's HTTP request (alternatively,
the controller might be aware of the command line arguments or last input
in a CLI). From this information, the controller should determine what view
to display, or what other application-related action should take place.
Controller conception is presented in current project through the number of
pages handlers. That is step out from the MVC conception, but in the same time
we reach through it some level of flexibility.
5. Setup
a) Edit setup.php
You must edit the values shown below:
/*** ADOdb library path */
define('LIB_ADODB', ROOT.'../adodb/');
/*** Smarty library path */
define('LIB_SMARTY', ROOT.'../smarty/');
b) Edit db/dbsetup.inc
You must edit the values shown below:
$dbHost = "localhost";
$dbUsername = "root";
$dbPassword = "0";
$dbName = "escort";
$go_conn = &ADONewConnection('mysql');
c) Then you need to run install/installdb.php to create tables
6. Example
The first thing that has to be added is:
require_once '../setup.php';
That needs to be placed before anything else in the page.
The next step is to compose template files and to define relations
between user actions and these temaplates.
// member actions
$actions["member_start"] = TEMPLATES.'member/member_page_start.tpl';
$actions["member_next"] = TEMPLATES.'member/member_page_next.tpl';
$actions["member_login"] = TEMPLATES.'member/member_login_form.tpl';
$actions["member_logout"] = TEMPLATES.'member/member_logout.tpl';
$actions["member_login_failed"] = TEMPLATES.'member/member_login_failed.tpl';
$actions["member_registrate_form"] = TEMPLATES.'member/member_registrate_form.tpl';
$actions["mainPage"] = TEMPLATES.'mainpage.tpl';
$actions["unknownPage"] = TEMPLATES.'unknownpage.tpl';
The third step is to modify coresponding handler - set controller reaction on
user's request:
$smarty = new Smarty;
switch ($page) {
case 'member_logout':
$user->logout();
break;
case 'member_start':
$smarty->assign("username",$_SESSION['username']);
break;
case 'member_next':
$smarty->assign("username",$_SESSION['username']);
break;
default: $page = 'unknownPage';
}
$smarty->display($actions[$page]);