Application:global configuration
From Surebert wiki
Contents |
Overview
The surebert framework takes an distributed approach to scope. Intentionally, there is very little globally scoped information and it is all stored in /private/config in App.php and definitions.php, both of which are described below. The goal is to have only the most necessary information load with each request making the system most efficient.
/private/config
definitions.php
This "global include file" is called before any requests are processed. Any functions, constants, and App class attributes defined in this file is globally available. You should only define data that is used on every single request, as this file is loaded for every request. Data that is specific to a certain view should be handled by the ViewClass for that view, in the .view template or in the Models the view deals with.
global CONSTANTS
Define any here using the function define(). These constants represent data that will never change in value after they are set. There should not be very many of these. Examples include DB_HOST, DEBUG_IP, etc. Constants should be defined in all caps.
You also define the path to the sbf files you will be using e.g.
php code
//Define the path to the surebert framework define("SUREBERT_FRAMEWORK_SB_PATH", "/var/www/sbf/sb/tags/1.49"); define("SUREBERT_FRAMEWORK_RP_PATH", "/var/www/sbf/rp/tags/1.43"); define("SUREBERT_TOOLKIT_PATH", "/var/www/sbf/toolkit/4.85");
global functions
Also in global scope, should be minimally used if at all. These functions are loaded for every single request. Examples would include print_raw from the old framework, although I suggest vardump() instead. Mostly, there should be no global functions but rather methods on the ViewClasses or Models.
global properties of the App Class
Set static props of the App class which are available in any scope and are loaded on every request. e.g. App::$user, App::$db These are essentially like the global constants but they can be variables that change and are accessed using the App classes scope resolution operator ::.
App.php
A static non-instantiated class that is available on every page request at any level, definitions.php, in models, and in views. Static properties must be pre-defined in App.php before being assigned in definitions.php or a fatal error will occurr.
App::$db
The main application database connection. Should be a sb_PDO or PDO reference. Other database connection can have more specific names. E.g. the connection to lawson_db would be App::$lawson_db
Here is an example connecting to mysql database, see the PDO manual for more information about PDO http://us.php.net/pdo
php code
App::$db = new sb_PDO("mysql:dbname=evals;host=".DB_HOST, DB_USER, DB_PASS);
You should probably use try/catch around this to provide a message and stop execution if the connection to the application database cannot be established.
php code
try{ App::$db = new sb_PDO("mysql:dbname=evals;host=".DB_HOST, DB_USER, DB_PASS); } catch( PDOException $e ){ die('Cannot connect to application database ;('); }
App::$user
The current user of the application. Should be an object instance, instantiated in definitions.php The use object will vary per system but should represent the current logged on user. Generally, a user should have at least the following properties.
Here is an example model you could use. Obviously you would have additional properties and methods that are specific to your app. Save this in you Models folder as User.php
php code
<?php /** * Respresents the most basic user in a web based system */ class User{ /** * The unique user id of the user * @var integer */ public $uid; /** * The username of the user (e.g. their handle) * @var string */ public $uname; /** * The english display name of the user e.g. Smith, Joe * @var string */ public $dname; /** * The email address of the user * @var string */ public $email; } ?>
Then in definitions.php you can instanciate the App::$user so that it available in all models/views/etc. Teh properties should be populated by data from your database that is associated with the user who is logged in.
php code
App::$user = new User();