iPinga
is the heart of this entire mini-framework.
The logic for developing an application using the iPinga
framework goes basically like this…
1) Create an instance of the iPinga
class
2) Tell that instance about the routes
you want your application to handle
3) Turn iPinga
loose to run whichever controller and method you established to handle the incoming request.
Example (sample index.php file)
1 2 3 4 5 6 7 8 9 10 11 |
require_once __DIR__ . '/vendor/autoload.php'; // Autoload files using Composer autoload $iPinga = new \ipinga\ipinga(); // Tell iPinga how to handle various requests $iPinga->addRoute('user/list','user','showList'); $iPinga->addRoute('user/add','user','add'); $iPinga->addRoute('user/get/$1','user','details'); $iPinga->defaultRoute('user','index'); $iPinga->run(); |
Sample ./controllers/user.controller.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<?php Class userController extends \ipinga\controller { /** * Normally this would be tied to a database, etc, but for the purposes of demonstration, * I am only wiring this up to an array */ protected $listOfUsers = array( 1 => 'David Burnet', 2 => 'Sam Houston', 3 => 'Mirabeau Lamar', 4 => 'Anson Jones' ); public function index() { echo 'this is the user/index controller/method<br/>'; echo '<form action="/user/add" method="post">'; echo ' First Name: <input type="text" name="first_name"><br/>'; echo ' Last Name: <input type="text" name="last_name"><br/>'; echo '<input type="submit"><br/>'; echo '</form>'; } public function showList() { $this->SendJSON($this->listOfUsers); } public function add() { echo 'This is user/add. Here is where you would normally<br/>'; echo 'add the form $_POST[] data to the database, but since we are<br/>'; echo 'not using a database we will just display what was posted via<br/>'; echo 'the form<br/><br/>'; echo $_POST['first_name']. '<br/>'; echo $_POST['last_name']. '<br/>'; } public function details($userIndex) { // bad ju-ju to not validate user supplied data, but this is a demo so... $this->json[$userIndex] = $this->listOfUsers[$userIndex]; $this->SendJSON(); } } ?> |
Given the above sample files, if the user went to your site at http://example.com, the default controller would be routed to. The resulting output would be:
If the user then supplied the name “John Magruder” then pressed the submit button, the form would post their data to http://example.com/user/add. The output
would look like this:
If the user went to the URL of http://example.com/user/get/4, the following would be the output:
Finally, if the user went to the URL of http://example.com/user/list, this would be the output:
Parent Class
None
Static Methods
Parameters:
None
Returns:
The
iPinga
object. An error will be thrown if you have not yet instantiated the iPinga
application objectExample:
1 |
$iPinga = \ipinga\ipinga::getInstance(); |
Public Properties
routes
. Under normal circumstances, you really shouldn’t access this directly. Use addRoute()
method instead.Data Type: array of routes
Default Value: Empty array
defaultRoute()
instead of accessing this directly, under normal conditions.Data Type: array
Default Value: Empty array
Public Methods
Parameters:
$overrideConfigOptions
(array: optional) list of configuration settings to override the defaults.Returns:
A
iPinga
instanceExample:
1 |
$iPinga = new \ipinga\ipinga(); |
iPinga
internal configuration options to the library defaultsParameters:
None
Returns:
null
Example:
1 2 3 4 5 6 7 |
$iPinga = new \ipinga\ipinga(); $iPinga->config('mysql.password','something new'); echo $iPinga->config('mysql.password'); // outputs: 'something new' $iPinga->setConfigOptionsToDefault(); echo $iPinga->config('mysql.password'); // outputs: ''your_db_password'' |
If only one argument is specified and that argument is a string, the value of the setting identified by the first argument will be returned, or NULL if that setting does not exist.
If only one argument is specified and that argument is an associative array, the array will be merged into the existing application settings.
If two arguments are provided, the first argument is the name of the setting to be created or updated, and the second argument is the setting value.
Parameters:
$array_or_key
(string or associative array) If a string, the key of the setting to set or retrieve. Else an associated array of setting keys and values
$value
If $array_or_key is a string, the value of the setting identified by $array_or_key
Returns:
The value of the settings
Example:
1 2 3 4 |
$iPinga = new \ipinga\ipinga(); $iPinga->config('mysql.password','something new'); echo $iPinga->config('mysql.password'); // outputs: 'something new' |
Parameters:
None
Returns:
A \PDO object
Example:
1 2 3 4 5 6 7 8 |
$iPinga = new \ipinga\ipinga(); $sql = 'select * from your_table'; // you might do bindParam() calls here before the prepare $stmt = \ipinga\pdo()->prepare($sql); $stmt->execute(); $rows = $stmt->fetchAll(); print_r($rows); |
Parameters:
None
Returns:
null
Example (sample index.php file)
1 2 3 4 5 6 7 8 9 10 11 |
require_once __DIR__ . '/vendor/autoload.php'; // Autoload files using Composer autoload $iPinga = new \ipinga\ipinga(); // Tell iPinga how to handle various requests $iPinga->addRoute('user/list','user','showList'); $iPinga->addRoute('user/add','user','add'); $iPinga->addRoute('user/get/$1','user','details'); $iPinga->defaultRoute('user','index'); $iPinga->run(); |
Parameters:
$urlToMatch
(string) is the matching information. When an incoming URL request matches, the associated controller/method is fired. Expected parameters should be listed in the matching string as starting with a dollar sign.$controller
(string) name of the controller to invoke if this URL matches the supplied $urlToMatch
matching information$method
(string) name of the method on the controller to invoke if this URL matches the supplied $urlToMatch
matching information$middleware
(string: optional) is a pipe separated list of middlewares to invoke prior to invoking the controller/method.Returns:
null
Example (sample index.php file)
1 2 3 4 5 6 7 8 9 10 11 |
require_once __DIR__ . '/vendor/autoload.php'; // Autoload files using Composer autoload $iPinga = new \ipinga\ipinga(); // Tell iPinga how to handle various requests $iPinga->addRoute('user/list','user','showList'); $iPinga->addRoute('user/add','user','add'); $iPinga->addRoute('user/get/$1','user','details'); $iPinga->defaultRoute('user','index'); $iPinga->run(); |
Parameters:
$controller
(string) name of the controller to invoke$method
(string) name of the method on the controller to invokeReturns:
null
Example (sample index.php file)
1 2 3 4 5 6 7 8 9 10 11 |
require_once __DIR__ . '/vendor/autoload.php'; // Autoload files using Composer autoload $iPinga = new \ipinga\ipinga(); // Tell iPinga how to handle various requests $iPinga->addRoute('user/list','user','showList'); $iPinga->addRoute('user/add','user','add'); $iPinga->addRoute('user/get/$1','user','details'); $iPinga->defaultRoute('user','index'); $iPinga->run(); |