-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Tessy nano framework is a set of tools that provide a boiler-plate for writing simple PHP apps quickly and efficiently. It is intended to help developers demonstrate, experiment and test code with ease and speed. It is nano as it only implements the fundamental ingredients for web apps in basic code.
Tessy offers the following features out of the box, but you can of course add yours as your project demands:
- Database connection
- Database query methods
- Data sanitization and validation
- HTML helpers
- Routing
- File uploading
- AJAX request handling
Clone/download and unzip Tessy into the root of your project directory. tessy.php is the core of Tessy while index.php is essentially a config file where you initialize your project.
Instantiate the Tessy class in index.php: $t = new Tessy();
.
To connect to a database, use:
$config = array(
"host" => "localhost",
"dbname" => "tessy",
"username" => "root",
"password" => ""
);
$t->odb( $config );
Close a database connection:
$t->cdb();
Routing is pretty easy with Tessy:
$t->addRoute( '/', function() { echo "Welcome Home!"; } );
$t->route();
NB: If your project is not in the root of your server, change the RewriteBase
in the .htaccess file appropriately i.e RewriteBase /projectFolder/
Also:
$t->addRoute( '/', 'myfunction' );
$t->route();
function myfunction() {
echo "Welcome Home!";
}
Use use to access your Tessy instance in an anonymous callback function or global in a non-anonymous one
$t->addRoute( '/about', function() use( $t ) { $t...; } );
$t->addRoute( '/api/books', 'apiFunc' );
function apiFunc() {
global $t;
$t...;
}
http://example.com/api/products/books/1/a0qXp3
In the above url, api is the route while the rest are parameters. You can access them with $r = $t->routeParams;. $r[1] = 'products', $r[2] = 'books', $r[3] = '1', and $r[4] = 'a0qXp3'. $r[0] = 'api'
.
Use routeElse to handle 404s:
$t->addRoute( '/a', 'functionA' );
$t->addRoute( '/b', 'functionB' );
$t->routeElse = "404 Error: Page not Found!";
$t->route();
Also:
$t->addRoute( '/a', 'functionA' );
$t->addRoute( '/b', 'functionB' );
$t->routeElse = function() { include "404.html"; };
$t->route();
NB: Define all routes in the index.php file.
Tessy provides 4 database query methods: create, read, update and delete - CRUD
Create/Insert
$table = "myTable";
$columns = array(
'col2' => 'tessy',
'col4' => mt_rand( 1, 100 )
);
$t->create( $table, $columns );
Read/Select
$table = "myTable";
$data = $t->read( $table ); // select all columns from myTable
$columns = [ 'col1', 'col2' ];
$data = $t->read( $table, $columns ); // select only col1 and col2
$clauses = "WHERE col1 > 0 ORDER BY col2 DESC LIMIT 20";
$data = $t->read( $table, [], $clauses );
echo "<pre>";
var_dump( $data['data'] ); // multidimensional array of results
echo "</pre>";
echo "Rows: " . $data['count']; // number of rows returned
Edit/Update
$set = array(
'col2' => 'beans',
'col3' => 'garri'
);
$clauses = "WHERE col1 = 1";
$t->edit( $table, $set, $clauses );
Delete
$clauses = "WHERE col1 > 5";
$t->delete( $table, $clauses );
Meta tags
$elements = array(
[ 'charset' => 'UTF-8' ],
[ 'name' => 'description', 'content' => 'A PHP Nano Framework' ],
[ 'name' => 'author', 'content' => 'Nicholas Kajoh' ]
);
$t->meta( $elements );
Favicon
$t->fav(); // if your icon is located in the your server root with file name of favicon.ico
// otherwise
$source = "images/icons/favicon/fav.png";
$type = "image/png"; // default = image/x-icon
$t->fav( $source, $type );
CSS
$base = "resources/css/"; // from project root
$sources = [ 'style', 'bootstrap' ]; // without the '.css'
$t->css( $base, $sources );
JavaScript
$base = ""; // from project root
$sources = [ 'test.min' ];
$t->js( $base, $sources ); // without the '.js'
Tessy's sanitization function cleans data by triming, adding slashes and removing html tags and comments. You can pass it a string or an array of strings.
$dirty = '<pre>I'll do my best</pre><script src="http://evilsite.com/evilscript.js" ></script>';
$clean = $t->sanitize( $dirty ); // output: I\'ll do my best
Validation: Tessy offers 5 validation data types:
-
uname
(username) - can start with _ or a-z, alphanumeric chars only, case insensitive, no spaces -
name
- alphabetic chars, hypens, apostrophes only, spaces, case insensitive -
email
-
number
- int or float -
url
$is_valid_username = $t->validate( '_000user', 'uname' ); // returns boolean (true/false) // also... $is_valid_name = $t->validate( 'James Bond', 'name' ); $is_valid_email = $t->validate( 'me.you@them.us', 'email' ); $is_valid_number = $t->validate( 2016, 'number' ); $is_valid_url = $t->validate( 'http://somesite.com/goodstuff', 'url' );
File uploading is a breeze with Tessy:
<input type="file" name="myfile">
$input_name = "myfile";
$params = array(
'name' => 'mynewfile',
'allowed_formats' => [ 'jpg', 'png', 'gif', 'raw' ],
'size_range' => [ 'min' => 12000, 'max' => 50000 ], // range in bytes
'upload_dir' => 'uploads/media/'
);
if( $t->upload( $input_name, $params ) ) echo "File uploaded!";
Get upload errors if any with $t->uploadErrors // array
To use ajax, define a route to handle your request and send your request to that route
// PHP
$t->addRoute( '/myajax', 'handleAjax' );
function handleAjax() {
var_dump( $_POST );
}
// JS
<script type="text/javascript">
$(function() {
$.ajax({
method: 'post',
url: '<?php echo $t->root ?>myajax',
dataType: 'text',
data: { msg: "Hello World" },
success: function(res) {
console.log( res );
}
});
});
</script>
Also
// PHP
$t->addRoute( '/myajax', 'handleAjax' );
function handleAjax() {
global $t;
var_dump( $t->routeParams );
}
// JS
<script type="text/javascript">
$(function() {
$.ajax({
method: 'get',
url: '<?php echo $t->root ?>myajax/id/1',
dataType: 'text',
success: function(res) {
console.log( res );
}
});
});
</script>