10 minute shout box with MiMViC using RedBeanPHP

Oct 21, 2010 MaXPert 5 Comments

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.

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 file index.php under your application directory. Open up the file in your favorite editor and require the both library files:

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 the author: MaXPert

Expert & Freelancer in PHP, MYSQL, Javascript, including many frameworks like codeigniter, cakephp, mootools, jquery, prototype js and a long list.

  • http://www.100webhosting.com Jasmine

    This is nice… although I have not programmed much in PHP, but the code looks neat and easy enough. Mimvic and RedbeanPHP look great!

  • Pingback: libro de visita en pocos minutos – con tbs « clicK-DIMension

  • http://www.webbgear.co.uk David

    Nice post – thanks.

  • http://tej-kohli-guide.livejournal.com Tej Kohli

    Such a great tips , I like to share it

  • p

    At first glance, this looks like a neat concept. I have my doubts however about deleting items via a GET request. Imagine what e.g. a Google bot will do when indexing this particular site…