Home » PHP » procedural MVC in php

procedural MVC in php

Well I am reading some question on http://programmers.stackexchange.com/questions/184664/mvc-pattern-on-procedural-php regarding procedural MVC in php. Then I am thinking why it should not be possible? After all MVC is about architecture, You can make good architecture also in procedural programming. You just need to design proper architecture. As I checkout on MVC Frameworks, their are some common terms like

  • Bootstrap script, which initializing system, core services, routers.
  • Controllers
  • Models
  • Views

Also library, helpers are available depend on project wise.

Now I am giving you some light on, How a MVC (very basic of course ) can be built on following style.

Architecture

Project Root
    config
        config.php
    lib
        core.php
    controllers 
        UserController.php
        BaseController.php
    models
        User.php
    views
        user 
            index.php
        base 
            welcome.php
    resources
        css
            style.css

config.php Sample Code :

define('MODEL_DIR', 'models');
define('VIEW_DIR', 'views');

$config = array (
    'default_module' => 'base', 
    'default_action' => 'index',
);

lib/core.php Sample Code :

function safe($param) {
    return addslashes($param);
}

function render($file, $data)
{
    $layout_file = VIEW_DIR.'/layouts/layout.php';
    ob_start();
    include_once($file);
    $content = ob_get_clean();
    include_once($layout_file);
}

Bootstrap index.php Sample Code :

Let Request url is like /index.php?module=user&action=index

require_once('config/config.php');
require_once('lib/core.php'); // For some core functions like render
//require_once('lib/db.php');   // For DB Abstraction [ Later Improvement ] 

$module = isset($_REQUEST['module'])?safe($_REQUEST['module']):$config['default_module'];
$action = isset($_REQUEST['action'])?safe($_REQUEST['action']):$config['default_action'];

$controller_file = 'controllers/'.ucfirst($module).'Controller.php';
if(!file_exists($controller_file))
{
    trigger_error('Invalid Controller');
    exit;
}
require_once($controller_file);
$function = strtolower($module).'_controller_'.$action;
if(!function_exists($function))
{
    trigger_error('Invalid Controller Action');
    exit;
}
call_user_func($function, $_REQUEST);

BaseController.php Sample Code :

function base_controller_index($request)
{
    render(VIEW_DIR.'/base/welcome.php', array());
}

UserController.php Sample Code :

function user_controller_index($request)
{
    require_once(MODEL_DIR.'/User.php');
    $data = user_model_list($request);
    render(VIEW_DIR.'/user/index.php', $data);
}

function user_controller_add($request)
{
}

function user_controller_edit($request)
{
}

function user_controller_view($request)
{
}

Model User.php Sample Code :

function user_model_list($request)
{
    return array(
        array('name' => 'test', 'sname' => 'test1', 'city' => 'testc'),
        array('name' => 'abc', 'sname' => 'xyz', 'city' => 'mno'),
    ); 
}

function user_model_add($request)
{
}

function user_model_edit($request)
{
}

function user_model_view($request)
{
}

Layout file views/layouts/layout.php Sample Code :

<html>
<head>
<link rel=’stylesheet’ href=’resources/css/style.css’ type=’text/css’ media=’all’ />
<head>
<body>
<div><h1>Logo</h1></div>
<div><h2>Heading</h2></div>
<div class=”content”>
<?php echo $content ?>
</div>
<body>
</html>

View views/base/welcome.php Sample Code :

<i>Welcome</i>
<p>
<a href=”?module=user&action=index”>User Listing Page</a>
</p>

View views/user/index.php Sample Code :

<table class=”listing”>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>City</th>
</tr>
<thead>
<tbody>
<?php foreach($data as $row) { ?>
<tr>
<td><?php echo $row[‘name’] ?></td>
<td><?php echo $row[‘sname’] ?></td>
<td><?php echo $row[‘city’] ?></td>
</tr>
<?php } ?>
<tbody>
</table>

Sample code could be found at https://github.com/kuldeep-k/procMVC

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s