Last time we had a look at the basics of MiMViC and PHP 5.3. Today we are going to build a cool database shoutbox application supporting all the basic CRUD operations in 10 minutes.
After you’ve downloaded the packages and configured your web server with required specs create a directory box under the root directory, so that files are available under your host at URI:
Under application directory extract the rb.php from RedBeanPHP and uvic.php from MiMViC (both of them should be placed at the root of application directory i.e.
Next we will be initializing our RedBean, for this tutorial we are going to use SQLite as database so adding our connection setup to the code our file will look like:
So lets go straight ahead and add basic listing and a form to add shouts to the database on root path of our application:
Uptill now we have just used the R::find() function to get all the shouts order them in descending order, and simply display the name and message with a link to delete the shout. As we can see the delete link is embedding the ID of the shout to be deleted. Second thing that you will notice is the form at top of list with two text fields (name and message) with a submit button; doing a POST to /box/add. So we need to implement a GET handler for /delete/:id request and POST /add. Lets go ahead and implement them as well.
To create shout under /add POST handler we are using R::dispense create shout, and R::store to save it, and then display message accordingly. Under /delete GET handler we are using R::findOne to digg out the target shout to delete and then R::trash to delete it and then display message accordingly. And this is it! Literally it’s a fully functional shoutbox within 10 minutes.
With the powerful constructs of RedBean and flexible architecture of MiMViC we created our application with some really cool looking URL’s and in no time at all!
Before you can really run the application make sure you have mod_rewrite enabled and .htaccess file with following contents in it:
After you have placed the .htaccess file go to URL http://localhost/box/ on your browser and enjoy the ride!
About MiMViC
MiMViC embraces the PHP 5.3 and takes a step into future by utilizing the namespaces, lambda functions and minimality. MiMViC is supposed to be super-light weight and programmer friendly framework, thus giving programmer only the most essential tools for programming. Practically speaking MiMViC follows the “KISS Rule”.A little introduction about RedBeanPHP
RedBeanPHP is an open source ORM (object relational mapping) tool for PHP. It focuses on simplicity and ease of use. What makes RedBean unique is that it creates your database schema on-the-fly. It scans your data and adjusts the column types to fit your object properties.Setup and Configuration
For this we will need the latest MiMViC, and ReadBeanPHP. (Please note that it requires you to have PHP 5.3 you can download XAMPP as pre-configured package).After you’ve downloaded the packages and configured your web server with required specs create a directory box under the root directory, so that files are available under your host at URI:
http://localhost/box/
. I will refer to the box
directory as application directory
from now onwards.Under application directory extract the rb.php from RedBeanPHP and uvic.php from MiMViC (both of them should be placed at the root of application directory i.e.
/box/uvic.php and /box/rb.php
).The index.php
Create fileindex.php
under your application directory. Open up the file in your favorite editor and require the both library files:- <?php
- require 'uvic.php';
- require 'rb.php';
- use MiMViC as mvc;
- ?>
- <?php
- require 'uvic.php';
- require 'rb.php';
- use MiMViC as mvc;
- R::setup('sqlite:dbase');
- ?>
- <?php
- require 'uvic.php';
- require 'rb.php';
- use MiMViC as mvc;
- R::setup('sqlite:dbase');
- mvc\get('/',
- function (){
- ?>
- <h1>Shoutbox!</h1>
- <form action="/box/add" method="POST">
- <input type="text" name="name" id="name" value="" />
- <input type="text" name="message" id="message" value="" />
- <input type="submit" value="add" />
- </form>
- <div>
- <?php
- $shouts = R::find('shouts', '1 = 1 order by id desc');
- foreach($shouts as $shout){
- ?>
- <div> <a href="/box/delete/<?php echo $shout->id;?>">X</a> <strong><?php echo $shout->name; ?>:</strong> <?php echo $shout->message; ?></div>
- <?php
- }
- ?>
- </div>
- <?php
- }
- );
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>MVC shoutbox</title>
- </head>
- <body>
- <?php mvc\start(); ?>
- </body>
- </html>
- <?php
- require 'uvic.php';
- require 'rb.php';
- use MiMViC as mvc;
- R::setup('sqlite:dbase');
- mvc\post('/add',
- function ($params){
- $sh = R::dispense('shouts');
- $sh->import($_POST, 'name,message');
- $id = R::store($sh);
- if($id){
- ?><script> window.location = '/box/'; </script><?php
- }else{
- echo 'Some problem occured';
- }
- }
- );
- mvc\get('/delete/:id', function($p){
- $shout = R::findOne('shouts', ' id = :id ', array(':id'=> $p['id']) );
- if($shout){
- if( !R::trash($shout) ){
- ?><script> window.location = '/box/'; </script><?php
- }else{
- echo 'Deletion failed';
- }
- }else{
- echo 'No such entity';
- }
- });
- mvc\get('/',
- function (){
- ?>
- <h1>Shoutbox!</h1>
- <form action="/box/add" method="POST">
- <input type="text" name="name" id="name" value="" />
- <input type="text" name="message" id="message" value="" />
- <input type="submit" value="add" />
- </form>
- <div>
- <?php
- $shouts = R::find('shouts', '1 = 1 order by id desc');
- foreach($shouts as $shout){
- ?>
- <div> <a href="/box/delete/<?php echo $shout->id;?>">X</a> <strong><?php echo $shout->name; ?>:</strong> <?php echo $shout->message; ?></div>
- <?php
- }
- ?>
- </div>
- <?php
- }
- );
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>MVC shoutbox</title>
- </head>
- <body>
- <?php
- mvc\start();
- ?>
- </body>
- </html>
With the powerful constructs of RedBean and flexible architecture of MiMViC we created our application with some really cool looking URL’s and in no time at all!
Before you can really run the application make sure you have mod_rewrite enabled and .htaccess file with following contents in it:
- RewriteEngine on
- RewriteCond $1 !^(index\.php)
- RewriteRule ^(.*)$ ./index.php/$1 [L]
No comments:
Post a Comment