What is MiMViC?
MiMViC is a modular and lightweight PHP 5.3+ Web application framework designed to help build dynamic and robust Web sites. It’s theory is based on minimal and framework emphasizes on only core glue for your complex PHP web sites.
Today I will show you how to create a shout-box using the MiMViC framework. It won’t take you more than 15 mins to get the job done and have friendly URLs as well.
Requirements
So here are couple of things that we need to get started:
- You are required to have PHP 5.3 for the framework to work. (Windows users can get a pre-configured package from here)
- A Copy of the MiMViC Framewok. (Download)
- A PHP language and mySQL knowledge
Now Let’s Build our Shoutbox!
In the root directory of your host create directory named shoutbox. Download and extract the uvic.php in the shoutbox directory. Now your shoutbox directory contains uvic.php file only. Next create a file named it index.php in the shoutbox directory, this file will initialize and include core components of mimvic. Paste the following code in the file:
|
1 2 3 4 5 6 7 8 9 |
<?php error_reporting(E_STRICT); require_once 'uvic.php'; use MiMViC as mvc; define('BASE_URL', '/shoutbox'); mvc\start(); ?> |
First line enables strict error reporting for php, next we include the uvic.php and after that in next line we import the mimvic namespace as mvc alias (just for making our code short and quick). Finally we define the BASE_URL constant so that we can later on use it to generate our URLs.
I will be using .htaccess file with mod_rewrite to have more sexy URLs so let’s create that as well in the shoutbox directory. Here are the contents of .htaccess, just copy paste them:
|
1 2 3 |
RewriteEngine on RewriteCond $1 !^(index\.php) RewriteRule^(.*)$ ./index.php/$1 [L] |
This will help me make my urls look like http://localhost/shoutbox/view/from/2/upto/100 making them look cleaner and still re-directs my actions to index.php. So the BASE_URL contains the /shoutbox part of URI in case when I am not using mod_rewrite it will be /shoutbox/index.php (and http://localhost/shoutbox/view/from/2/upto/100 will become http://localhost/shoutbox/index.php/view/from/2/upto/100 ).
So let us install a hello world action and see if that works:
Add following lines of code before the mvc\start() call in index.php.
|
1 2 3 4 5 6 7 8 9 10 11 |
mvc\post('/add', function ($params){ echo ‘you called add’; } ); mvc\get('/', function (){ echo ‘Yay its on fire!’; } ); |
We are utilizing the full potential of PHP 5.3 here and registering two callback functions on these relative URIs (relative to http://localhost/shoutbox since our source lies there). Now hit the http://localhost/shoutbox/ and http://localhost/shoutbox/add to see the resulting echo from the registered functions.
Pretty simple and minimal, right? Now let’s connect to DB and get down some serious business. Lets remove the above two callback actions and make sure our index file has only these contents:
|
1 2 3 4 5 6 7 8 9 |
<?php error_reporting(E_STRICT); require_once 'uvic.php'; use MiMViC as mvc; define('BASE_URL', '/shoutbox'); mvc\start(); ?> |
For shout box create a database named shoutbox you can use following SQL to create the table:
|
1 2 3 4 5 6 |
CREATE TABLE "shouts" ( "id" int(10) unsigned NOT NULL AUTO_INCREMENT, "name" varchar(100) NOT NULL, "shout" varchar(255) NOT NULL, PRIMARY KEY ("id") ); |
Once we have created table, we can come back to shoutbox folder and create a conf.php containing following code:
|
1 2 3 4 5 6 7 |
<?php require_once 'uvic.php'; use MiMViC as mvc; $dbh = new PDO("mysql:host=localhost;dbname=shoutbox","[username]","[password]"); mvc\store('db', $dbh); ?> |
Again we just import the uvic.php and rename the namespace as done before. Next we create a PDO connection to database using the [username] and [password] credentials. The interesting part is we store this connection in mimvic storage facility so that later we can retrieve its value by mvc\retrieve(‘db’) anywhere globally in code, so rather than using global variable we will be asking MiMViC to store and retrieve values.
Now let’s create another file shout_actions that contains all the shout actions. Here lets register an action handler to view first 10 shouts, so paste following code in shouts_action.php:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php namespace ShoutActions; require_once 'uvic.php'; use MiMViC as mvc; mvc\get('/', function (){ $query = 'select * from shouts order by id desc limit 0,10’; $stmt = mvc\retrieve('db')->prepare($query); $data = array(); if(!$stmt->execute()) $data['error'] = true; else $data['shouts'] = $stmt->fetchAll(); mvc\render('views/view-shouts.php', $data); } ); ?> |
In viewShouts function I have retrieved the latest 10 shouts, And made an array $data that will be passed to our view as data. So the view file must lie in views directory named view-shouts.php as passed to render function, here are the contents of view-shouts.php:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <div id="shouts"> <?php if( !isset($error) ): ?> <?php foreach($shouts as $shout): ?> <div class="shout"> <b><?php echo $shout['name'] ?></b> <p> <?php echo $shout['shout'] ?> </p> </div> <?php endforeach ?> <?php else: ?> <div class="error"> An Error occured try refreshing </div> <?php endif ?> </div> </body> </html> |
As you can see the variable stored at the ‘shouts’ index is available as variable here and similar is the matter with ‘error’; I have used the values inside them, iterated over them and displayed the entries. Let’s add the delete and add shout functionality for this I am going to add following piece of code to my shout_actions.php.
|
1 2 3 4 5 6 7 8 9 |
mvc\post('/save', function (){ $stmt = mvc\retrieve('db')->prepare("insert into shouts(name, shout) values (:name, :shout)"); $status = $stmt->execute( array('name' => $_POST['name'], 'shout' => $_POST['shout']) ); if(!$status) die("System die : ("); header("Location: ".BASE_URL.'/'); } ); |
The above action will save the shout posted via POST method, so code is pretty self-explanatory, now let’s add the delete functionality and this time we are going to use URL parameters to pass parameters to it, and since we will invoke it URL in browser I will register it with get method, Here is the code:
|
1 2 3 4 5 6 7 8 9 10 |
mvc\get('/delete/:id', function ($params){ $stmt = mvc\retrieve('db')->prepare("delete from shouts where id = :id"); $status = $stmt->execute( array('id' => $params['id']) ); if(!$stmt->execute()) die("false"); header("Location: ".BASE_URL.'/'); } ); |
The :id in URI tells mimvic to parse out the parameter passed in this segment of URI, and passes it at ‘id’ index of the associative array being passed to the callback function as parameter. Once I receive that parameter I plugged it into the SQL statement and executed it; once executed successfully I redirect back to root page. Now, we can add the following code for showing shout add form in view-shouts.php.
|
1 2 3 4 5 |
<form id="formArea" action="<?php echo BASE_URL?>/save" method="post"> Name: <input id="name" name="name" type="text" value="" /><input type="submit" value="Shout" /><br/> Shout: <input id="shout" name="shout" type="text" value="" style="width: 300px;" /> </form> <hr /> |
Similarly we can add the delete shout URL to the loop on $shouts so the block will look like:
|
1 2 3 4 5 6 7 8 9 |
<?php foreach($shouts as $shout): ?> <div class="shout"> <b><?php echo $shout['name'] ?></b> <p> <?php echo $shout['shout'] ?> </p> <a href="<?php echo BASE_URL?>/delete/<?php echo $shout['id']?>">Delete</a> </div> <?php endforeach ?> |
Now, our basic shoutbox application is now ready. However a demo is not possible in this host because I am running a PHP version lower that 5.3.x. Download the framework and more examples here
Subscribe in my RSS Feed for free updates.

Tags: 




Pingback: Embracing PHP 5.3 MVC with MiMViC | INSIC DESIGNS | Coder Online
Pingback: 網站製作學習誌 » [Web] 連結分享
Pingback: 10 minute shout box with MiMViC using RedBeanPHP | INSIC DESIGNS
Pingback: libro de visita en pocos minutos – con tbs « clicK-DIMension
Pingback: links for 2011-05-27 at Here I Rule
Pingback: 10 minute shout box with MiMViC using RedBeanPHP | XFloyd's blog