The Model-View-Controller programming framework provides the ability to organize code to absolutely minimize the new code that must be written. I choose to use it whenever possible. Heavyweight MVC frameworks include Ruby On Rails (RoR) and Akelos, a PHP port of RoR. I have had some projects that are too small to justify these frameworks, so I created my own, tinyMVC. It provides a wrapper around the packages of software that I use for my different projects.

Installing tinyMVC

Its general layout is like this:
    .
    |-- apache
    |-- docs
    |   `-- GNU General Public License.txt
    |-- lib
    |   |-- autoLoad.php
    |   |-- FrameView.class.php
    |   |-- StringView.class.php
    |   `-- tinyMVC.php
    `-- vendor
        `-- restserver
    
I say "general" because many of the items in my tinyMVC are not shown in this listing for brevity sake. Here is what we have:
 
Creating a tinyMVC project


The general layout of a tinyMVC project is like this:
    .
    |-- apache
    |-- app
    |   |-- controllers <-- PHP files here
    |   |-- installers
    |   |-- models
    |   `-- views       <-- HTML templates here
    |-- config
    |   |-- config.php
    |   `-- routes.php
    |-- data
    |-- docs
    |   `-- readme.txt
    |-- public
    |   |-- index.php
    |   |-- php_errors.log
    |   `-- stylesheet.css
    |-- script
    `-- test
    
Here is a description of its contents:
  • apache - This is a small script that changes permissions and owner/group so that a project can be run in a web environment. It contains these two lines
          chown apache:users . -R
          chmod g+rw . -R
          
    and must be run as root. I need it for my local server, but not for my remote server. Of course, the details may be different on your machine.
  • app - This directory is where most of your application code goes. PHP code goes into the controllers subdirectory. HTML code goes into the views subdirectory. Code that is used to set up your project, such as create a database, goes in the installers subdirectory. Code that is used to control what gets written to data sources goes in the models subdirectory. Keep in mind that it is your project. There are no strict rules.
  • config - There are two files in the config directory, config.php and routes.php.
    • config.php contains file paths, database info such as host name, user name, password and server name. In this example, DS is defined in the project's public/index.php file. It's value is "\" on a Windows machine; otherwise DS is defined as "/".
      define('ROOT',DS.'home'.DS.'alan'.DS.'develop'.DS.'php'.DS);
      define('PROJECT_ROOT_DIR',ROOT.'demos'.DS.'my_project'.DS);
      define('TINY_MVC_DIR',ROOT.'tinyMVC'.DS);
      • ROOT describes the common path to both the project and tinyMVC. Your project and tinyMVC don't have to have a common path, but that's how I do it.
      • PROJECT_ROOT_DIR describes the path to the project's root.
      • TINY_MVC_DIR describes the path to tinyMVC.

      When a project is to be installed on a remote server after being tested on a local server, one could write two sets of defines, one for each in an "if" block that identifies which is which. If you have such a block, you'll want to have your database login information here, too.

    • The routes.php file contains addMap statements for the RestServer. You write these to control RESTful navigation through your project. An example of such a statement is
      $rest->addMap("GET","/?","HomeController");
  • data - This directory is for your project's data. An example of use is a sqlite database.
  • docs - This directory holds the documentation for your project.
  • public - This directory holds the index.php file, the php_errors.log and your stylesheets. If you put your project in a place that is not accessible to Apache, you need to create a link from the accessible area (/var/www/html or httpdocs) to this public directory.
    • index.php is provided with tinyMVC. It doesn't need to be changed. The reason for that is that it includes the two files in your config directory, which you do change.
    • php_errors.log is created by Apache if you set up your httpd.conf file to do so. You should periodically empty this file if you want to be able to find your latest errors.
    • stylesheets must be in this public directory, for Apache needs to access them.
  • The script directory is where scripts created for your project are stored.
  • The test directory is where your unit testing scripts should be stored.
 
tinyMVC Tips


From the POST HTTP method in one controller to the GET method of another controller
$rest->getResponse()->cleanHeader();
$rest->getResponse()->addHeader("Location: ?q=/");
return $rest;

The location will be one of those you've defined in routes.php.
RestServer will use that header to check the map which will tell it which controller (and possibly method) to execute.

Your test yields a blank screen.

Check your php.ini file to make sure that you are producing an error file. The error file will be written in the your_project/public/ directory. That should be your first line of analysis. (I found that out the hard way.)

 
More Documentation and Examples
 

Here are tinyMVC downloads, zipped up and ready to be installed on your server. There is a bare-bones version of tinyMVC with only the RestServer package installed. There are three examples that were written by RestServer's author, Diogo Souza da Silva, and modified by Alan Lake to fit in and work with tinyMVC. In their docs directories are explanations for how they work. restserver_ex2, in particular, has documentation written into the code, so you can examine how it works as it is working. To use these examples on your local server, you'll have to create links from Apache's "document root" to their directories called "public".

More detailed information in the use of RestServer is found in the tinymvc/docs directory.

Bare-bones tinyMVC
Restserver Example 1, Example 2 and Example 3.