Launched a new PHP framework - Simple MVC Famework

David Carr

Development Nova Framework Tools

I've been working on a new framework for quite some time, I've dedicated to release it for anyone to use.

simplemvcframework.com

I started building the framework as I wanted to start using a framework, when I looked at the choices of current systems they all look good but come with way too much baggage I wanted something much lighter that doesn't have a complicated setup.

I think this framework fits that, its big enough to not be classed as a micro framework but smaller then mainstream frameworks. Its smaller due to the small amount of included helper classes whilst its nice to have everything you can think of included into a framework a lot of the time there not needed.

The following classes are included by default

  • Database
  • Pagination
  • Sessions
  • Url

The framework uses an autoloader, only the used classes are loaded.

Simple MVC Framework is a PHP 5.3 MVC system. Its designed to be lightweight and modular, allowing developers to build better and easy to maintain code with PHP.

For further details on the framework see the Documentation

Installation is very simple

  1. Download the framework
  2. Unzip the package.
  3. Upload the framework files to your server. Normally the index.php file will be at your root.
  4. Open the index.php file with a text editor and set your base URL and database credentials (if a database is needed). Set the default theme and controller to be loaded.
  5. If using a Linux server edit .htaccess file and save the base path.

A simple example

Here's a very quick example of using the framework, I'll create a page that lists contacts stored in a database.

First create a controller call it contacts.php in /controllers/ A call the the parent constructor is made, this gives the controller access to views and models, set a method (index) this will run as soon as contacts in called in the URL.

Set the page title and call a method from a model then pass the array to a view.

class Contacts extends Controller {

    //call the parent constuctor
    function __construct(){
        parent::__construct();
    }

    //index methods automatically run when calling the class such as http://domain.com/contacts
    public function index(){

        //set the data array
        $data['title'] = 'Contacts';
        $data['records'] = $this->_model->getContacts();

        //include a view and pass in the array
        $this->_view->render('contacts/list',$data);
    }

}

Inside the models folder create a model called contacts_model.php again call the constructor then create a method and setup a db query return the records for the controller to use.

class Contacts_Model extends Model {

    //call the parent constuctor
    function __construct(){
        parent::__construct();
    }

    public function getContacts(){
        return $this->_db->select("SELECT firstName,lastName,email FROM ".PREFIX."contacts");
    }

}

Next in the views folder create a folder called contacts in there add a file called list.php

The view will check if a key exists in the data array is it does its then used.

<?php if($data['title']){ echo $data['title']; } ?>
<table>
<tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Email</th>
</tr>
<?php
if($data['records']){
    foreach($data['records'] as $row){
        echo "<tr>";
            echo "<td>$row->firstName</td>";
            echo "<td>$row->lastName</td>";
            echo "<td>$row->email</td>";
        echo "</tr>";
    }
}
?>
</table>

Visits simplemvcframework.com for more information.</p>

Read articles directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Copyright © 2006 - 2024 DC Blog - All rights reserved.