4
* Fight: an extension to Flight, in the form of useful configuration changes
4
* Slight: an extension to Flight, in the form of useful configuration changes
5
5
* and extra library code.
7
7
* @copyright Copyright (c) 2015, Tim Marston <tim@ed.am>
10
* FIGHT! ...brings the following changes to Flight:
12
* 1. Global $app object.
14
* 2. App directorties.
10
* Slight brings the following changes to Flight:
12
* 1. A global $app object.
16
16
* It is expected that your app has the following layout, and class and view
17
17
* paths are set up to reflect this:
55
55
* NOTE: the use of default values for arguments is REQUIRED, or a PHP error
56
56
* would occur where if they were missing in the HTTP request.
58
* You can also specify either "special cases" of actions, or actions for a
59
* lower level of the URL path, by using a double-underscore to represent a path
60
* separator in your action method name. For example, using the above routing,
61
* /some/path/list/1 will route to MyClass::action_list__1(), if it exists,
62
* leaving URLs that have other values in the last part of their path (for
63
* example, /some/path/list/2) to still route to MyClass::action_list() as
58
* You can also write "specialisations" of actions, where an action has a
59
* specific argument value. This is achieved by using a double underscore in
60
* the method name to represent the path separator. For example, using the
61
* above routing, /some/path/list/1 will route to MyClass::action_list__1(), if
62
* it exists, leaving URLs that have other values in the last part of their path
63
* (for example, /some/path/list/2) to still route to MyClass::action_list() as
64
64
* before (passing 2 as the first argument).
66
66
* Also note that hyphens in URLs are converted to underscores (or they wouldn't
67
* be usable as method names), and multiple underscores are contracred to a
67
* be usable as method names), and multiple underscores are contracted to a
68
68
* single underscore (so that the path separators can be represented as a
69
* double-underscode in method names)
69
* double-underscore in method names)
71
* 4. Add database connection and classes
73
* The new method connect() takes an array of connection parameters, as follows:
74
* type: the database type (defaults to mysql)
75
* host: the database hostname (defaults to localhost)
76
* port: database port number (optional)
77
* dbname: database name
78
* username: authentication user
79
* password: authentication password
80
* prefix: a table prefix
82
* This makes a PDO connection available at $app->db().
84
* A Model base class is available, for an app to base it's own models on.
88
* The following helper functions are provided:
91
* This is a global function which escapes text for use in HTML.
92
* $app->link( 'some/where' )
93
* Constructs and returns links, taking in to account the base_url.
72
// instantiate flight app
97
// instantiate global flight app
73
98
require 'flight/autoload.php';
74
99
$app = new flight\Engine();
76
// set fight class path
101
// set slight class path
77
102
$app->path( __DIR__.'/classes' );
80
105
$app->set( 'flight.views.path', 'app/views' );
81
106
$app->path( 'app/controllers' );
107
$app->path( 'app/models' );
84
110
$app->set( 'flight.log_errors', true );
124
155
// check to see if combined parts make a method name
125
156
$method = 'action_'.implode( '__', $parts );
126
if( method_exists( $class, $method ) ) {
157
if( method_exists( $class, $method ) )
159
// set controller url
160
$controller = explode( '/', preg_replace(
161
array( '/^\//', '/\/$/' ), '',
162
$app->request()->url ) );
163
array_splice( $controller, -count( $params ) );
164
$app->set( 'controller',
165
join( '/', $controller ) );
167
// instantiate class and call method
127
168
$obj = new $class();
128
169
return call_user_func_array(
129
170
array( $obj, $method ), $params );
146
// add $app to view parameters
187
// add $app to global view parameters
147
188
$app->view()->set( 'app', $app );
190
// add connect() method
191
$app->map( 'connect',
192
function( $params ) use( &$app )
195
$params += array( 'type' => 'mysql', 'host' => 'localhost' );
197
// construct pdo string
198
$pdostr = $params[ 'type' ].':host='.$params[ 'host' ];
199
if( isset( $params[ 'port' ] ) ) $pdostr .= ';port='.$params[ 'port' ];
200
$pdostr .= ';dbname='.$params[ 'dbname' ];
203
$app->register( 'db', 'PDO',
204
array( $pdostr, $params[ 'username' ], $params[ 'password' ] ) );
207
if( isset( $params[ 'prefix' ] ) ) {
208
$app->set( 'dbprefix', $params[ 'prefix' ] );
212
// add general html escaping function
215
return htmlspecialchars( $text, ENT_COMPAT | ENT_HTML5 );
220
function( $url = null ) use( &$app )
222
if( is_null( $url ) ) $url = $app->get( 'controller' );
223
$url = preg_replace( array( '/^\//', '/\/$/' ), '', $url );
224
return $app->get( 'base_url' ).'/'.$url;