Download & Setup
Use Composer. To setup a new PHPTree project
composer create-project phptree/phptree
Life is simple!
Server Requierments
- PHP >= 8.0.0
- PDO PHP Extension
- OpenSSL PHP Extension
Inside the framework
Routing SQL/PDO Logger Debugger Secure JWT Redis Memcached Files cacheYour First Project
😍 Congrats!. After you manage to download the framework and created a new project, you can see a demo classes which located inside "app" directory.
find "initialize.php" its a class which initialized on load , you can add more classes or change it by modify auto loaded classes , now open "initialize.php" and explore our demo and sample script.
Project Environment
You need to modify default project settings by editing .env.json file , which is located inside project root directory
prod | Boolean | Production or development mode , true for production mode |
debug | Boolean | Enable or disable debugger |
system | array | System information , such as domain name |
Autoload classes
PHPTree will try to load included and required classes using provided directories inside "classmap".
Load more classes on by adding them inside "init".
classmap | array | (Array of strings) , example ["app/", "src/"] |
init | array | (Array of strings) startup Initialize classes , example ["app\\initialize"] |
Route caching type
Define cacheType for routes, which can be a value from 0 to 3 :
cacheType | int |
|
JWT Token
A secret Token used to decrypt data using Secure class.
You can use THIS TOOL to generate a new Token.
JWT_SECRET | string | a secret token for JWT |
Caching
PHPTree support 3 kinds of caching , you can edit, enable or disable them.
file | bool | Enable or disable file caching |
redis | array | Redis server information |
memcached | array | Memcached information , with a list of available servers |
Logs
Do not miss a warning , exception or error .
errors | string | A full path for error and exceptions logs file , default : "var/logs/errors.txt" , "" to disable |
mysql | string | A full path for mysql exceptions logs file , default : "var/logs/mysql.txt", "" to disable |
Routing
It is easier more than you think!
Basics
Basic route example
Route::get("Path");
OR
Route::post("Path");
Where "Path" is a browser or API routing , it can be anything you want.
Lets setup a method for our "Path"!
Route::get("Path")->setMethod("app\\views@home");
The modifier setMethod can be a class method , static method or a closure .
Modifiers
Modifier | Input | Info |
---|---|---|
where | ( array $keys ) | Array of accepted params regex for dynamic route |
enabled | ( bool $isEnabled ) | Enable / Disable |
setMethod |
|
|
setGroup | ( null | string $group ) | Group name |
redirect | ( string $to , int $code = 302) | Redirect route to another path or domain |
Example
Here is a full example how to use Route modifiers.
use PHPTree\Core\PHPTreeRoute as Route;
Route::get("/profile/{id}")
->setMethod("app\\admin\\views@user_profile")
->setGroup("user_profile")
->where(['id' => '[0-9]+'])
->enabled(true);
namespace app\admin;
class views {
public function user_profile( $params ){
echo " User profile request for ID : " . $params['id'];
}
}
Secure Requests
Keep your project safe , by using Secure class
safePost | ( $encoding = "UTF-8" ) | Return XSS escaped $_POST |
safeGet | ( $encoding = "UTF-8" ) | Return XSS escaped $_GET |
safeRequest | ( $encoding = "UTF-8" ) | Return XSS escaped $_REQUEST |
safeArray | ( array $array , $encoding = "UTF-8" ) | Return XSS escaped array |
safeInput | ( string $string , $encoding = "UTF-8" ) | Return a XSS escaped string |
Example :
use PHPTree\Core\PHPTreeSecure as Secure;
$post = Secure::safePost();
//This is safe
$name = $post['name'];
JWT Encryption / Decryption
RSA is a popular algorithm for asymmetric (public key) encryption that was established more than 40 years ago. Encrypting a JWT for a given recipient requires their public RSA key. The decryption takes place with the corresponding private RSA key, which the recipient must keep secret at all times.
Example for Data Encryption :
$post_token = Secure::generateToken( ['name'=>'Jack' , 'gender' => 'male'] );
Example for Data Decryption :
$data = Secure::validateToken( $post_token );
File cache
Use Debugger to manage cached files.
set | ($key , $array , $timestamp = 0) | Set key value and its timeout |
get | ($key) | Return value or false |
exists | ($key) | Return true or false |
delete | ($key) | Remove cached key |
flush | ( bool $force = false ) | Remove all keys or expired only |
allKeys | Return all keys |
Example :
use PHPTree\Core\PHPTreeCache AS PTCACHE;
PTCACHE::set("name" , "jack" , 3600);
echo PTCACHE::get("name");
Redis
Require Redis to be installed on your server. Use Debugger to manage cached info.
set | ($key , $array , $timestamp = 0) | Set key value and its timeout |
get | ($key) | Return value or false |
exists | ($key) | Return true or false |
delete | ($key) | Remove cached key |
flush | () | Remove all keys |
Example :
use PHPTree\Core\PHPTreeCacheRedis AS PTREDIS;
PTREDIS::set("name" , "jack" , 3600);
echo PTREDIS::get("name");
Memcached
Require Memcached to be installed on your server. Use Debugger to manage cached info.
set | ($key , $array , $timestamp = 0) | Set key value and its timeout |
get | ($key) | Return value or false |
exists | ($key) | Return true or false |
delete | ($key) | Remove cached key |
flush | () | Remove all keys |
Example :
use PHPTree\Core\PHPTreeCacheMemcached AS PTMEMCACHED;
PTMEMCACHED::set("name" , "jack" , 3600);
echo PTMEMCACHED::get("name");
MySQL PDO
Require a PDO extension to be installed and enabled.
Establish database connection Example :
use PHPTree\Core\PHPTreePDO AS DB;
$db = new DB( array('host' => 'mysql:host=localhost;dbname=phptree' , 'username' => 'root' , 'password' => 'root') );
now we can use "$db" to manage our data inside the "phptree" database.
Select All
$users = $db->select("SELECT * FROM users WHERE class = :cname ", [ [":cname" , "math" , \PDO::PARAM_STR] ] );
Select One
$user = $db->selectOne("SELECT * FROM users WHERE id = :id ", [ [":id" , $id , \PDO::PARAM_INT] ] );
Count
$users_in_class = $db->count("SELECT * FROM users WHERE class = :cname ", [ [":cname" , "math" , \PDO::PARAM_STR] ] );
Delete
$db->delete("DELETE FROM users WHERE class = :cname ", [ [":cname" , "math" , \PDO::PARAM_STR] ] );
Update
$db->update("UPDATE users SET class = :cname ", [ [":cname" , "math" , \PDO::PARAM_STR] ] );
Query
$db->query("UPDATE users SET class = :cname ", [ [":cname" , "math" , \PDO::PARAM_STR] ] );
Insert
$db->insert("INSERT INTO users (`username`) VALUES (:username) ", [ [":username" , "Jack" , \PDO::PARAM_STR] ] );
Debugger
You can trace your project speed , warnings , errors and more by using PHPTree Debugger , you can enable or disable debugger from .env.json
Also you can add your custom debug Tab inside your executed code by adding the following :
use PHPTree\Core\PHPTreeLogs AS Logs;
Logs::addTab( string $title , string | array $content , int $badge = 0 );