Help

Installation

How to get a copy of StaticPHP?

Download or clone it from GitHub

How to install it?

  1. Extract the archive (skip it if you cloned git repository)
  2. Point webserver's root directory to the application/public directory
  3. Set directory index file to index.php

Usage

Framework base is built using php static methods

Document root (application/public) Configuration (application/config) All configuration files are put in application/config directory. config.php is the main application config file and is loaded automatically. routing.php is where application routes are set. View each configuration file for more details. Controllers (application/controllers) Models (application/models) Helpers (application/helpers) Views / Templates (application/views) StaticPHP uses default php templating system, which means no parsing to views is done to improve its performance.

Controller:
$data = ['msg' => 'Important message'];
\load::view(['header', 'home/index', 'footer']);
    
View: (application/views/home/index)
'); ?>
    

System

bootstrap.php

router.php

Function Reference
  • public static function base_uri($url = '')
  • public static function site_uri($url = '', $prefix = NULL, $current_prefix = TRUE)
  • public static function make_path_string($string)
  • public static function redirect($url = '', $site_uri = TRUE, $e301 = FALSE, $type = 'http')
  • public static function have_prefix($p)
  • public static function segment($index)
  • public static function error($error_code, $error_string, $data = NULL)
  • public static function url_to_file($url)
  • public static function init()
  • public static function split_segments($force = FALSE)
  • public static function load_controller()
  • public static function _load_controller($file, &$class, &$method)

load.php

Function Reference
  • public static function &get($name)
  • public static function set($name, $value)
  • public static function merge($name, $value, $owerwrite = TRUE)
  • public static function config($files, $project = NULL)
  • public static function controller($files, $project = NULL)
  • public static function model($files, $project = NULL)
  • public static function view($files, &$data = [], $return = FALSE, $project = NULL)
  • public static function helper($files, $project = NULL)
  • public static function start_timer()
  • public static function stop_timer($name)
  • public static function mark_time($name)
  • public static function execution_time()
  • function __autoload($classname)

Models

To load in-built models use \load::model('model name', 'system');

system/models/db.php

Simple pdo wrapper

Connecting to the database
\load::model('db', 'system');
\models\db::init([
  'string' => 'mysql:host=localhost;dbname=',
  'username' => '',
  'password' => '',
  'charset' => 'UTF8',
  'persistent' => TRUE,
  'wrap_column' => '`', // ` - for mysql, " - for postgresql
  'debug' => FALSE,
]);

// or

\models\db::init(); // database class will load default configuration, assuming that database model and configuration files were auto-loaded
      
Selecting
\models\db::fetchAll('SELECT * FROM users WHERE user_type = ?', 1);

\models\db::fetch('SELECT * FROM users WHERE user_id = ?', $user_id);
      
Inserting
\models\db::query('INSERT INTO users SET name = ?, email = ?', ['Gints', 'gm@gm.lv']);

\models\db::insert('users', [
  'name' => 'Gints',
  'email' => 'gm@gm.lv'
]);
      
Updating
\models\db::query('UPDATE users SET name = ?, email = ? WHERE user_id = ?', ['Gints', 'gm@gm.lv', $user_id]);

\models\db::update('users', [
  'name' => 'Gints',
  'email' => 'gm@gm.lv'
], ['user_id' => $user_id]);
      
Function Reference
  • (PDO) public static function init($config, $name = 'default')
  • (PDOStatement) public static function query($query, $data = NULL, $name = 'default')
  • (object) public static function fetch($query, $data = [], $name = 'default')
  • (array) public static function fetchAll($query, $data = [], $name = 'default')
  • (PDOStatement) public static function update($table, $data, $where, $name = 'default')
  • (PDOStatement) public static function insert($table, $data, $name = 'default')
  • (PDO) public static function &db_link($name = 'default')
  • (PDOStatement) public static function &last_statement()
  • (string) public static function last_query()
  • (mixed) public static function last_insert_id($sequence_name = '', $sql = FALSE, $name = 'default')

Helpers

To load in-built models use \load::helper('helper name', 'system');

Nginx configuration example

/etc/nginx/sites-enabled/gm.lv
server {
    listen       80;
    server_name  staticphp.gm.lv;

    root   /www/sites/gm.lv/staticphp/application/public;
    index index.php index.html index.htm;

    location / {
        if (!-e $request_filename)
        {
            rewrite . /index.php  last;
        }
    }


    location ~ \.php$ {
        if (!-f $request_filename) {
            return 404;
        }

        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }


    location ~ /\.ht {
        return 404;
    }
}
/etc/nginx/fastcgi_params
fastcgi_param	QUERY_STRING		$query_string;
fastcgi_param	REQUEST_METHOD		$request_method;
fastcgi_param	CONTENT_TYPE		$content_type;
fastcgi_param	CONTENT_LENGTH		$content_length;

fastcgi_param	SCRIPT_FILENAME		$request_filename;
fastcgi_param	SCRIPT_NAME		$fastcgi_script_name;
fastcgi_param	REQUEST_URI		$request_uri;
fastcgi_param	DOCUMENT_URI		$document_uri;
fastcgi_param	DOCUMENT_ROOT		$document_root;
fastcgi_param	SERVER_PROTOCOL		$server_protocol;

fastcgi_param	GATEWAY_INTERFACE	CGI/1.1;
fastcgi_param	SERVER_SOFTWARE		nginx/$nginx_version;

fastcgi_param	REMOTE_ADDR		$remote_addr;
fastcgi_param	REMOTE_PORT		$remote_port;
fastcgi_param	SERVER_ADDR		$server_addr;
fastcgi_param	SERVER_PORT		$server_port;
fastcgi_param	SERVER_NAME		$server_name;

fastcgi_param	HTTPS			$https;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param	REDIRECT_STATUS		200;

Example application

This Page

Some variables and definitions:
  1. BASE_URI:
  2. \router::site_uri():

  3. BASE_PATH:
  4. APP_PATH:
  5. PUBLIC_PATH:
This page:
  1. Controller:
  2. View:
  3. CSS:
 
All included files: