PHPEasy is an API-centric PHP framework. Code as close to PHP language itself rather than a framework.
- REST API development
- Full-stack web app development
- File-based routing
- Lightweight, no too much dependencies and configurations
- Simple Database Abstraction Layer
- Helper Functions such as input validator, code generator, upload file, etc.
- Promotes ubt not limitted to procedural programming
- Supports PHP 8 and above , MySQL, MSSQL and SQlite
- Includes basic css(mystyle.css) and js(vanscript.js) utility library.
- Promotes to master the PHP language itself rather than a framework.
- Direct to the point coding, no too much abstractions.
- Use of Data Abstraction Layer rather than ORM, focused on maximum performance without large database calls used by orms.
I. Intro
II. Pre-requisites
III. Installation
This is for someone who loves Vanilla PHP and its simplicity. Nowadays you must follow coding standards(OOP,SOLID,DRY,etc.) and mvc frameworks to do web development using PHP. PHP frameworks out there comes with too much files, configurations, classes and dependencies. I made this mini framework so php developers can do web development faster while mastering and enjoying the PHP language itself (Yes! no need to learn so many libraries).
- PHP 8^.
- Composer.
Composer - open a terminal inside your root or htdocs folder and execute the command below.
composer create-project vgalvoso/phpeasy my_phpeasy
you can change [my_phpeasy] to any project name you want.
Now open your browser and go to http://localhost/my_phpeasy
Create views inside View folder.
View routes will be automatically created based on View folder structure.
Look at examples below.
- View file path: [View/admin/dashboard.php], the route: ["admin/dashboard"].
- View file path: [View/login.php], the route: ["login"].
You can ommit the file name if the view file is named [index.php]:
- View file path: [View/index.php], the route: [""].
- View file path: [VIew/admin/index.php], the route: ["admin"].
(SPA)Single Page Applications are composed of view components.
View components are accessible only through ajax requests.
Just call Core/Helper/component(); at the top of view file to specify it as a view component.
Example: View/admin/users_table.php
<?= Core/Helper/component(); ?>
<table>
<thead>
<tr>
<th>Username</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody id="users_tbl">
<tr><td>No Contents</td></tr>
</tbody>
</table>
PHPEasy supports REST API.
All APIs are placed inside api folder.
API routes will automaticaly created through api folder file structure and implemented functions inside the php file named with http verbs e.g.(get(),post(),patch()).
So for example you omitted the delete() function, you can't call DELETE api/users/{id}.
Here is an example of a Users REST API.
API file path: [api/users.php]
Routes:
- GET api/users - Get all users
- GET api/users/{id} - Get user by id
- POST api/users - Create new user
- DELETE api/users/{id} - Delete a user
- PUT api/users/{id} - Replace user
- PATCH api/users/{id} - Update a user
<?php
use Core\DAL;
use function Core\Helper\error;
use function Core\Helper\getRequestBody;
use function Core\Helper\response;
use function Core\Helper\startAPI;
startAPI();
function get(){
$db = new DAL();
//GET user by id
if(defined('URI_PARAM')){
$query = "SELECT * FROM users WHERE id = :id";
$param = ["id" => URI_PARAM];
if(!$user = $db->getItem($query,$param))
response([]);
response([$user]);
}
//GET All users
$query = "SELECT * FROM users";
$users = $db->getItems($query);
response($users);
}
function post(){
$db = new DAL();
$rq = (object)getRequestBody();
$values = [
"username" => $rq->username,
"firstname" => $rq->firstname,
"lastname" => $rq->lastname,
"usertype" => $rq->usertype,
"password" => password_hash($rq->password,PASSWORD_BCRYPT)
];
if(!$db->insert("users",$values))
error($db->getError());
response("New User added!");
}
function delete(){
if(!defined('URI_PARAM'))
error("Invalid Request! Please specify user id");
$db = new DAL();
$id = URI_PARAM;
if(!$db->delete("users","id=:id",["id" => $id]))
error($db->getError());
response("User Deleted Successfuly!");
}
function patch(){
if(!defined('URI_PARAM'))
error("Invalid Request! Please specify user id");
$db = new DAL();
$id = URI_PARAM;
$rq = (object)getRequestBody();
$values = [
"firstname" => $rq->firstname,
"lastname" => $rq->lastname];
$params = ["id" => $id];
$db = new DAL();
if(!$db->update("users","id=:id",$values,$params))
error($db->getError());
response("User Updated Successfuly");
}
//EOF
APIs in PHPEasy encourages a procedural coding style,
so here are the list of functions that you can use in API implementations:
Initialize a PHP file as a REST API.
After calling this function you can implement http verbs as function.
Example:
<?php
use function Core\Helper\startAPI;
startAPI();
function get(){
//Handle GET request to api/users
}
Error response will be received if you try to request using http methods other than GET.
Get request body and convert it into assoc array.
Example:
<?php
use Core\Helper\getRequestBody;
$rq = getRequestBody();
$username = $rq["username"];
$password = $rq["password"];
//you can convert it to object for easy access
//$rq = (object)$rq;
//$username = $rq->username;
//$password = $rq->password;
Validate a key-value pair array based on validation rules.
- Return true if valid, exit and return 400 status code and error details if not.
- Use it to validate request data ($_GET,$_POST).
$inputs
- Associative array to be validated.$validations
- Associative array containing keys that matched keys in $data and values are the validation rules.
Example:
<?php
use function Core\Helper\getRequestBody;
use function Core\Validator\validate;
$rq = getRequestBody();
$dataRules = ["uname" => "required|string",
"upass" => "required|string",
"firstName" => "required|string",
"lastName" => "required|string"];
validate($rq,$dataRules);
Output response with 400 status code and error message
$message
- String|Array Error Message
Set content type and status code then output content and exit script
$content
string|array - The content to output$statusCode
int - The response status code (default 200)$contentType
string - The content type (default application/json). Available content-types: [ application/json | plain/text | text/html ]
Include specified view
$route
string - View file path
Mostly used for calling SPA component
Redirect to specified view.
If path is not specified, redirect based on session.
$view
string - Path to view
Shorter syntax for htmlspecialchars()
$string
- String to sanitize- Use it for HTML sanitization.
Generate a randomized alphanumeric code
$length
- Length of code to be generated (default 6)
Extract object keys and values and store to session array
$object
- The object to extract Example:
<?php
use Core\DAL;
$db = new DAL();
if(!$user = $db->getItem(1))
invalid("User does not exist!");
objToSession($userInfo);
Generate new file name and upload the file
string $uploadFile
$_FILE key
string $uploadPath
Location for upload file must add "/" to the end- returns boolean|string New file name if successful, false otherwise
Get/Set a session variable
$sessionVar
- Session Key$value
- Sets a session value if null
Convert an array of objects to indexed array containing values of specified item.
$objArr
- Array if ibjects to convert$item
- object item to extract
PHPEasy introduces DAL() class for database operations. Supports MYSql, MSSql and SQLite.
Set database configurations in Config/Database.php
Below are DAL() functions
$db = new DAL();
Executes an INSERT statement;
$table
- The table name to be inserted$values
- Associative array containing keys that match table fields and the values to insert.- returns boolean
Example:
$values = ["username" => $uname,
"password" => $upass,
"firstname" => $firstName,
"lastname" => $lastName];
$db->insert("users",$values);
Executes update statement
string $table
The table to updatestring $condition
Conditions eg. id = :idarray $values
Associative array containing values to update eg .["age" => 27]
array $params
Values for conditions eg . ["id" => 1]- return boolean
Example:
$values = [
"firstname" => $firstName,
"lastname" => $lastName];
$params = ["id" => 1];
$db = new DAL();
$db->update("users","id=:id",$values,$params);
Executes delete statement
string $table
The table to delete fromstring $condition
Conditions using prepared statement eg. id = :id AND name = :namearray $params
Values for conditions eg. ["id" => 1,"name" => "Juan Dela Cruz"]- return boolean
Example:
$delete("users","id=:id",["id" => 1]);
Select multiple items
string $query
Select statementarray $inputs
Parameters for prepared statement default(null)- return array|false
Example:
$db = new DAL();
$sql = "SELECT * FROM users WHERE lastname = :surname";
$params = ["surname" => $lastName];
$users = $db->getItems($sql,$params);
Select single row query
string $query
Select statementarray $inputs
Parameters for prepared statement default(null)- return object|false
Example:
$db = new DAL();
$sql = "SELECT * FROM users WHERE id=:userId";
$params = ["userId" => 1];
$users = $db->getItem($sql,$params);
Start a database transaction.
Commit database transaction.
- Place this before returning a response in api.
Rollback database transaction.
- Rarely used because when you exit the script without calling commit(),
- rollback() will be automatically executed.
Returns Database Errors
- A very useful debugging tool.
Get lastId inserted to database
string $field
Specify a lastId field, default null
Get the database friver that is currently used.
- api/getAllUser.php
```php
<?php
$db = new DAL();
$users = new Users($db);
$usersList = $users->getAll();
You can use models or not depending on project requirements.
DAL class is accessible directly in api files, you can execute a query directly on api implementation without creating a Model.
PHPEasy is progressive, you can add Models, Services if you like, just update the composer.json file if you added other directory.
See
- js helpers: public/js/vanscript.js
- css: public/css/mystyle.css