III. MVC Framework

Eden does not ship with an MVC framework because there are already a lot of great PHP frameworks available. If you still insist to use our framework we use here at Openovate Labs, right on ! and please read on.

If you haven't done so, please download our MVC project found here. You will need to upload it to a server with PHP 5.3. First off make sure you point your DNS to the web folder. With Apache, you need to open your httpd.conf file and add the following template to the end.

Figure 1. httpd.conf
<Directory "/path/to/project">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

<VirtualHost yourwebsite.com:80>
    ServerName yourwebsite.com
    ServerAdmin your@email.com
    DocumentRoot /path/to/project/web
</VirtualHost>

It's important to replace yourwebsite.com to your actual DNS and /path/to/project to the actual path you extracted the MVC at. The Directory tag tells your operating system to give Apache explicit permissions to use this folder. The VitualHost tag tells apache if someone tries to access this server by yourwebsite.com then load the index file located at /path/to/project. If you do not have a registered DNS like yourwebsite.com you'll need to modify your computer's hosts file. In windows this is found at C:\windows\system32\drivers\etc\hosts. For Unix this is usually found at /etc/hosts. Figure 2 shows an example of what to enter.

Figure 2. hosts File
127.0.0.1 yourwebsite.com
Make sure Figure 2 is made on a new line.

If you visit your website you should see something like the image below.

The next thing we need to understand is the folder structure as depicted below.

web

The web directory is also known as our demilitarized zone (DMZ). Since we stategically pointed our server to this folder, all other folder are safely behind to prevent unwanted directory browsing. In your DMZ you should put things like images, style sheets etc. things that don't change and should be freely accessable. We also put our control driver called front.php. Our goal for this controller is to route any URL request to this file. We are able to do this with a file called .htaccess also found in the web directory.

module

The module directory is for reusable models, handlers, factory and blocks that surrounds the concept of a model. A model file only handles one instance or one row. There maybe times where a model can be associated with an event handler for example an email model. There are also times where a model could be associated with some sort of output block like an RSS entry. There are also times where we just need to retieve a list of models. For all of these reasons, we have the module folder to store all of this valuable functionality to be reused across different applications. We'll go over more about this subject in 2. Modeling

front

The front folder is where we store our application specific functionality and in this case having to do with a public web output. In the tradition of an MVC framework, we only want to gather data from the defined models to be used for an output of some sort. If we wanted to create a mobile application for example we would need to copy all the contents in the front folder to another folder called mobile.

config

The config directory is for any environmental specific configuration that can be used across different applications. If you open database.php you will see a commented out key value array where your supposed to put in your database connection info.

Next, let's connect a MySQL database to the project. Open config/database.php, remove the comments and enter your MySQL connection info. An example entry can be found in Figure 3.

Figure 3. database.php
return array (
	'default' => array(
		'host' 	=> '127.0.0.1',
		'name' 	=> 'eden',
		'user' 	=> 'root',
		'pass' 	=> '',
		'type' 	=> 'mysql',
		'default' => true));

Next execute the following query from Figure 4 to create some database tables.

Figure 4. MySQL Schema
CREATE TABLE IF NOT EXISTS `post` (
  `post_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `post_user` int(11) unsigned NOT NULL,
  `post_title` varchar(255) DEFAULT NULL,
  `post_detail` text,
  `post_active` smallint(1) unsigned NOT NULL DEFAULT '1',
  `post_created` datetime NOT NULL,
  `post_updated` datetime NOT NULL,
  PRIMARY KEY (`post_id`),
  KEY `post_user` (`post_user`),
  KEY `post_title` (`post_title`),
  KEY `post_active` (`post_active`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `user` (
  `user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `user_email` varchar(255) NOT NULL,
  `user_name` varchar(255) DEFAULT NULL,
  `user_password` varchar(255) NOT NULL,
  `user_active` int(1) unsigned NOT NULL DEFAULT '1',
  `user_created` datetime NOT NULL,
  `user_updated` datetime NOT NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `user_email` (`user_email`),
  KEY `user_active` (`user_active`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

INSERT INTO `post` (`post_user`, `post_title`, `post_detail`, `post_created`, `post_updated`) VALUES
(1, 'my draft', 'okay a draft', '2012-05-12 15:41:56', '2012-05-12 15:41:56');
INSERT INTO `user` (`user_email`, `user_name`, `user_password`, `user_created`, `user_updated`) VALUES
('sample@email.com', 'Sample User', '202cb962ac59075b964b07152d234b70', '2012-05-12 15:41:56', '2012-05-12 15:41:56');

Check to make sure you have 2 tables in your database. We will be using these tables in the following sections. When you have a general understanding of the file structure, your free to move on to our next section 1. The Controller, where we will cover more about the controller.


© 2012 Openovate Labs. All rights reserved.