Installation
Back in time (2 more)
Home | Pages | Files

SimpleMVC Installation Guide

get packages files

get latest simpleMVC files

For now as simpleMVC is still under developpment and waiting for a stable realease (don’t mistake it’s already used on productions sites) the best way is to get files by using svn. Supposing you want to install the full package in your root web server path (here /var/www) just type:

user@host$ svn export http://svn.jgotti.net/simpleMVC/trunk /var/www/

Now you have the Controller and View part of the MVC pattern not the Model one for the Model part just keep reading. (now class-db project was incorporated as an external inside simpleMVC so you get the model part too) And a sandBox application.

get class-db and/or abstractModels classes

this is not required anymore since class-db and abstactModels is now part of simpleMVC as an external repository

application configuration

Now that we have all our files we must do a little configuration to make all work properly.

htaccess

the _.htaccess file is only there to redirect user to the default application of our choice and to enable RewriteRules. If you don’t want to use RewriteRules in your application you can just delete the _.htaccess file and skip to next part project config file.

Still reading so you intend to use RewriteRules. you will need to rename the file _.htaccess to .htaccess and set a correct value to RewriteBase.

#- set error document to a file you want to manage redirection errors
ErrorDocument 500 /index.php
#- set apache rewrite engine to on
RewriteEngine on

#- this must be configure to work properly in any environment (RELATIVE PATH TO THE WEBSERVER DOCUMENT_ROOT)
RewriteBase /

#- optional setting for redirection to default application when no param given (here the default application is sandBox)
RewriteRule ^(?:index.php(.*))?$ sandBox$1 [R,L]

#--- rest of the file doesn't need to be touched ---#

That’s all for the htaccess file.

project config file

Each application managed by simpleMVC can have it’s own config file. it will be placed in the config dir and will be named projectName_config.php so in case of sandBox application we will have a config/sandBox_config.php file.
If many of your applications has common configuration directives, they can be defined only once in a generic config.php file, consider this one as default settings.

Configuration files are read at the start of every pages and parsed by fx-conf, all directives will be defined as PHP constants in your application.
Directives that are already declared can be re-used for other directives declaration by encapsuling them between to ‘%’ symbol. See APP_URL in our sample config for a living example.

Here’s a sample configuration for our sandbox application.

#- <?php echo "Access Denied!"; exit(); ?>
#- @package simpleMVC
#- @subpackage config
#- GENERAL CONFIG FOR APPS
#- FRONT_NAME, ROOT_URL, LIB_DIR and CONFIG_DIR are already defined when config file is parsed so we can use them to declare our new directives.
#- ADD ANY CONSTANTS YOU MAY REQUIRE HERE

#- ACTIVATE/DEACTIVATE DEVELOPPMENT STATE (DEBUG LEVEL)
#- this will set class-db verbosity, the use or not of the dbProfiler, displaying or not the devel bar and the treatment of general catchable exceptions in controllers.
DEVEL_MODE = true

#- DEFAULT DISPATCHING
#- this is the name of the default controller and default action when not defined in the request_url, separated by a ':'
DEFAULT_DISPATCH = pages:index
ERROR_DISPATCH = pages:error

#- DATABASE CONNECTION
#- this is a class-db common connection string see class-db for more infos
DB_CONNECTION  = mysqlidb://dbname;dbhost;dbuser;dbpass

#- SOME PATH CONFIGURATION REQUIRED OR USEFUL FOR FURTHER DEVELOPMENT.
#- root url is the root_path of all our applications managed by MVC.(no trailing slash)
ROOT_URL     = http://example.com/
#- app url and app dir point to the application directory.(no trailing slash)
APP_URL      = %ROOT_URL%/sandBox
APP_DIR      = %ROOT_DIR%/sandBox

#- DO WE USE REWITES RULES
#- this setting will impact how url_viewHelper will compute url and must be set to false if you don't use apache rewrite engine
USE_REWRITE_RULES = true

#- PATH USED FOR FILEMANAGER HELPER
USER_DATAS_DIR = %ROOT_DIR%/files
USER_DATAS_URL = %ROOT_URL%/files

#- CACHE CONFIG
CACHE_MANAGER_ENABLE = false
CACHE_MANAGER_DEFAULT_BACKEND = fileCacheBackend
CACHE_MANAGER_AUTOCLEAR = true
CACHE_MANAGER_TTL = 30 minutes
CACHE_FILE_ROOT_DIR = %APP_DIR%/views/_cache
CACHE_FILE_USE_SUBDIRS = false

bootStrap settings

What we call bootStrap is the index.php file that is called every time a page is requested inside a simpleMVC application.
It is in this file that all required files are load, config is parsed and dispatch is made. It is the entry point of the simpleMVC application, where all things are put together. It’s there we will sets the behaviour of the core of the MVC such as langManager config, view engine and default layout and so on.

Some of you familiar with other MVC framework may ask where is what some other MVC frameworks call a dispatch controller. To keep things simple, simpleMVC doesn’t have a real dispatchController, in fact the dispatch is done inside the bootStrap.
This allow us to easily change the disaptching methods without the need of using other complexity

In fact commonly used settings are already there in the bootstrap, you just have to uncomment lines regarding what you need and configure them correctly if needed. here’s our sandBox bootStrap file:

<?php
/**
* @package simpleMVC
* @subPackage sandbox
*/
#- next three lines depends on your needs they correspond to my developpment settings and are optionals, they could have be defined in the php.ini for example.
error_reporting(E_ALL | E_STRICT);
ini_set('default_charset','utf-8');
date_default_timezone_set('Europe/Paris');

#- name of the execution context, in fact the application name, most of the time i like to use the parent directory name.
define('FRONT_NAME',basename(dirname(__file__)));

#- common paths definition 
define('ROOT_DIR',dirname(dirname(__file__)));
define('CONF_DIR',ROOT_DIR.'/config');

#- include common function set and most important, will declare the autoloader and parse configs files.
require '../includes/fx-common.php';

#- starting session in corresponding context and ensure each MVC application has it's own session
if( isset($_SESSION) )
 session_write_close();
session_name(FRONT_NAME);
session_start();

#- if needed specified your default database connection (uncomment next two lines)
#- db::setDefaultConnectionStr(DB_CONNECTION);
#- db::$_default_verbosity = DEVEL_MODE?1:0; #- only report errors

#- include class-abstractModel if you use them (uncomment next two lines)
#- require LIB_DIR.'/class-abstractmodel.php';
#- abstractModel::$useDbProfiler = DEVEL_MODE?true:false;

#- Set default views directories lasts will be try first and vice-versa 
abstractController::$defaultViewClass = 'baseView';
abstractController::$defaultViewDirs  = array(
 LIB_DIR.'/views',
 APP_DIR.'/views'
);

#- setting default layout 
baseView::$defaultLayout = array(
  'header.tpl.php',
  ':controller_:action.tpl.php|default_:action.tpl.php',
  'footer.tpl.php'
);

#- if multilingual then setup langManager
#- first set directories for dictionaries lookUp
#-langManager::$localesDirs = array(
#- ROOT_DIR.'/locales',
#- APP_DIR.'/locales',
#-);
#- then set current lang in session
#- if( isset($_SESSION['lang']) )
#- langManager::setCurrentLang($_SESSION['lang']);
#- else
#- $_SESSION['lang'] = langManager::langDetect(true);

#- set default dictionary for model filters messages (usefull only if you use abstractModels and langManager in the same app)
#- abstractModel::$dfltFiltersDictionary='filters';

#- dispatching you don't need to edit following lines 
if( USE_REWRITE_RULES ){
 if((!isset($_SERVER['PATH_INFO'])) && isset($_SERVER['REDIRECT_QUERY_STRING']) ){
  $_SERVER['PATH_INFO'] = preg_replace('!^([^\?&]+).*$!','\\1',$_SERVER['REDIRECT_QUERY_STRING']);
  if(isset($_GET[$_SERVER['PATH_INFO']]) && empty($_GET[$_SERVER['PATH_INFO']])){
   unset($_GET[$_SERVER['PATH_INFO']]);
  }
 }
 if( isset($_SERVER['PATH_INFO']) ){
  $route = explode('/',substr($_SERVER['PATH_INFO'],1));
  $_controller = count($route) ? array_shift($route) : null;
  $_action = count($route) ? array_shift($route) : null;
  while (count($route) > 1) {
   $_GET[array_shift($route)] = array_shift($route);
  }
 }
}

list($_defaultController,$_defaultAction) = explode(':',DEFAULT_DISPATCH);
#- get requested controller and action
$_controller = isset($_POST['ctrl'])?$_POST['ctrl']:(isset($_GET['ctrl'])?$_GET['ctrl']:(!empty($_controller)?$_controller:$_defaultController));
$_action     = isset($_POST['action'])?$_POST['action']:(isset($_GET['action'])?$_GET['action']:(!empty($_action)?$_action:$_defaultAction));

#- controller instanciation
try{
 $cname = $_controller.'Controller';
 if( class_exists($cname) )
  $controller = new $cname;
}catch(Exception $e){
 if( DEVEL_MODE )
  show($e->getMessage(),'color:orange;trace;exit');
 abstractController::appendAppMsg($e->getMessage(),'error');
 $controller = new defaultController;
 $controller->redirectAction('error','default',null,404);
}
#- action call
try{
  $controller->$_action();
}catch(Exception $e){
 if( DEVEL_MODE )
  show($e->getMessage(),$e->getTrace(),'color:maroon;trace;exit');
 abstractController::appendAppMsg($e->getMessage(),'error');
 $controller->redirectAction('error','default',null,404);
}