Core Boxx – Lightweight PHP Rapid Development Framework

 

TABLE OF CONTENTS

Download & License Core Boxx Modules What Is Core Boxx?
Requirements & Overview Quickstart Tutorial Core Function Summary

 

 

DOWNLOAD & LICENSE

Download Core BoxxSource Forge

Core Boxx is released under the MIT License. You are free to use it for your own personal and commercial projects, modify it as you see fit. On the condition that there the software is provided “as-is”. There are no warranties provided and “no strings attached”. Code Boxx and the authors are not liable for any claims, damages, or liabilities.

 

CORE BOXX MODULES

Go ahead, check out the Core Boxx modules. Download the ones you need, plug into Core Boxx, do some of your own customizations, and help yourself speed up development.

  • Mail – Send emails with ease.
  • Resumable Upload – Resumable uploads with FlowJS and FlowPHP.
  • Dynamic Menu – Create a dynamic HTML menu with database.
  • Dynamic Content – Store and retrieve contents from the database.
  • Users – User database and management functions.
  • Forgot Password – An “extension” of the user module, automated password recovery.

 

 

WHAT IS CORE BOXX?

Core Boxx is a super lightweight PHP development framework. It’s so compact that there are only 2 folders, 2 .htaccess files, and 2 PHP scripts – Weighing in at less than 6KB. The whole idea of Core Boxx is not to be another one of those overly bloated frameworks with tons of unused features, bogging down the performance of the system. So yes, Core Boxx is built with 2 things in mind:

  • Minimalistic: No functions and features bloating, no distractions. Only the raw essentials that will help speed up development.
  • Modular: Only develop what you need, extend only when the module is required.

Personally, I think Core Boxx is more like a “minimal pre-defined system core” rather than a full-fledged framework.

 

REQUIREMENTS & OVERVIEW

Core Boxx should pretty much work with any modern AMP (Apache MySQL PHP) servers. Requires Apache mod_rewrite, and the PHP PDO extension to be enabled. That’s all. As for the “framework” itself, there are only 2 folders:

  • lib/ Where the core script and library are placed in, where we create more module scripts.
  • api/ The API endpoint for AJAX and server-to-server communications.

 

 

QUICK KICKSTART TUTORIAL

Don’t worry, Core Boxx does not have some crazy documentation that takes months to read… Just this page. How can it be used to help develop web applications quickly? Let us walk through a quick example of creating a user module (click here to download all the tutorial code).

 

STEP 1) CHANGE THE SETTINGS

lib/Core.php
// (D) SETTINGS
// (D1) DATABASE SETTINGS - CHANGE TO YOUR OWN!
define('DB_HOST', 'localhost');
define('DB_NAME', 'test');
define('DB_CHARSET', 'utf8');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
 
// (D2) HOST - CHANGE TO YOUR OWN!
define('HOST_BASE', 'http://localhost/');

Captain Obvious step 1. Open the core script, and change the database settings to your own. Doh. Tweak the rest of the settings if you want… If you are having trouble with the auto path, or want to change error reporting.

 

STEP 2) CREATE DATABASE TABLE

users.sql
CREATE TABLE `users` (
  `user_id` int(11) NOT NULL,
  `user_email` varchar(255) NOT NULL,
  `user_password` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 
ALTER TABLE `users`
  ADD PRIMARY KEY (`user_id`),
  ADD UNIQUE KEY `user_email` (`user_email`);
 
ALTER TABLE `users`
  MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;CODE

Just a simple dummy users table here with 3 fields:

  • user_id The primary key.
  • user_email The user’s email, unique field.
  • user_password Self-explanatory.

 

 

STEP 3) DEVELOP THE LIBRARY MODULE

lib/Users.php
<?php
class Users extends Core {
  // (A) SAVE USER
  function save ($email, $password, $id=null) {
    // (A1) NEW USER
    if ($id === null) {
      return $this->core->insert(
        "users", ["user_email", "user_password"], [$email, $password]
      );
    }
    // (A2) UPDATE USER
    else {
      return $this->core->update(
        "users", ["user_email", "user_password"], "`user_id`=?", [$email, $password, $id]
      );
    }
  }

  // (B) GET USER
  function get ($id) {
    return $this->core->fetch(
      "SELECT * FROM `users` WHERE `user_". (is_numeric($id)?"id":"email") ."`=?", [$id]
    );
  }

  // (C) VERIFY
  function verify ($email, $password) {
    // (C1) GET USER + VERIFY PASSWORD
    $user = $this->get($email);
    $pass = is_array($user);
    if ($pass) { $pass = $password == $user['user_password']; }

    // (C2) RESULTS
    if (!$pass) {
      $this->error = "Invalid user/password.";
      return false;
    } else { return true; }
  }
}

With the database table in place, the next step is to create the PHP library itself.

  • Since we are creating a user module, we create a corresponding lib/User.php – Take note of the capital U.
  • Set the class name to the same as the file name, and remember to extend the core – class User extends Core.
  • All that’s left is to add the functions. In this dummy library, we have 3 simple functions:
    • function save() Add or update a user.
    • function get() Get a user by ID or email.
    • function verify() Verify the given email and password (for login).
  • Take note of how it “links” back and uses functions from the core library $this->core->insert(), $this->core->update(), $this->core->fetch(), and more.
  • If you are curious about how this works, dissect function extend() and class Core in the lib/Core.php in your own free time… If not, just know that you can use $this->core->XXX to access the core library.
  • A list of the core functions will be summarized below.

P.S. Modules can also access each other. For example, we can create another Cipher module. So when saving the user passwords, we can use the encryption function – $this->core->extend("Cipher"); $password = $this->core->Cipher->encrypt($password);

 

 

STEP 4) USE THE LIBRARY

user-test.php
<?php
// (A) LOAD CORE + EXTEND USERS MODULE
require "lib/Core.php";
$_CORE->extend("Users");
 
// (B) ADD NEW USER
echo $_CORE->Users->save("john@doe.com", "lousypassword") ? "OK" : $_CORE->error ;
 
// (C) UPDATE USER
echo $_CORE->Users->save("jane@doe.com", "lousypassword123", 1) ? "OK" : $_CORE->error ;
 
// (D) GET BY ID OR EMAIL
$user = $_CORE->Users->get(1);
print_r($user);
$user = $_CORE->Users->get("jane@doe.com");
print_r($user);
 
// (E) VERIFY USER PASSWORD
echo $_CORE->Users->verify("jane@doe.com", "lousypassword123") ? "OK" : $_CORE->error ;

The database and library are in place, all that’s left is to use it in your own scripts.

  • Very simply, require "lib/Core.php" in your own script to load Core Boxx.
  • To load the user module that we just created – $_CORE->extend("Users").
  • All the user functions can now be accessed at $_CORE->Users->FUNCTION().

 

EXTRA EXAMPLE) LOGIN PAGE

user-login.php
// (A) PROCESS LOGIN
if (isset($_POST['email'])) {
  require "lib/Core.php";
  $_CORE->extend("Users");
  if ($_CORE->Users->verify($_POST['email'], $_POST['password'])) {
    /* DO YOUR LOGIN SEQUENCE
    $_SESSION['user'] = ...;
    header("Location: HOME PAGE");
    exit();
    */
    echo "<div>OK</div>";
  } else { echo "<div>{$_CORE->error}</div>"; }
}
?>
 
<!-- (B) LOGIN FORM -->
<form method='post'>
  <input type='email' name='email' required placeholder='Email'/>
  <input type='password' name='password' required placeholder='Password'/>
  <input type='submit' value='Login'/>
</form>

For the people who might still be lost on “how to use it in HTML page” – Here is another quick example of a login page.

P.S. Core Boxx is a PHP framework, not an HTML/CSS/Javascript framework. It does not dictate how to build your webpages, nor does it limit which client-side framework you use.

 

 

STEP 5) DEVELOPING THE API ENDPOINT (OPTIONAL)

Actually, the above steps pretty much covered a fully functioning system. But for the people who are looking to create an endpoint for AJAX or server-to-server calls, here is a simple user endpoint.

api/user.php
<?php
$_CORE->extend("Users");
switch ($_REQ) {
  default: 
    $_CORE->respond(0, "Invalid request", null, 400); // Bad request
    break;
 
  case "get":
    // http://site.com/api/user/get/?id=1
    $user = $_CORE->Users->get($_GET['id']);
    $_CORE->respond(is_array($user), "ok", $user);
    break;
 
  case "update":
    // http://site.com/api/user/update/?email=foobar%40doe.com&password=123&id=1
    if ($_CORE->Users->save($_GET['email'], $_GET['password'], $_GET['id'])) {
      $_CORE->respond(1, "User Updated"); 
    } else { $_CORE->respond(0); }
    break;
}
  • First, take note of the format of the URL for API calls – http://site.com/api/MODULE/REQUEST/. For example, if we want to get a user, it can be http://site.com/api/user/get/?id=999.
  • In api/index.php, the path segment of the URL will be parsed into the array $path and later separated out – $_MOD holds the requested module, and $_REQ holds the requested action.
  • $_MOD will be used to try to find the respective handling script. In this case, http://site.com/api/user/get/ will look for api/user.php.
  • The above example script should now make sense – We are simply using switch($_REQ) and $_CORE->Users->XXX to handle the various requests accordingly.
  • Lastly, take note of how $_CORE->respond() is used to help output a JSON encoded response – Don’t need to stick with this though. Feel free to formulate and come up with your own “standard server responses”.

 

CORE FUNCTIONS SUMMARY

With that, we have come to the end of the “quickstart tutorial”, and here is a summary of the core functions (plus quick examples).

 

Database Core
Function Description & Quick Example
__construct() Connects to the database when the $_CORE object is created.
__destruct() Closes the database connection when done.
connect() Connect to the database.
start() Auto-commit off.

function save () {
  $this->core->start();
  $pass = $this->core->update(ENTRY 1);
  if ($pass) { $this->core->update(ENTRY 2); }
  $this->core->end($pass);
  return $pass;
}
end($pass) Used in conjunction with start().

  • $pass – Boolean, commit or rollback?
query($sql, $data) Runs an SQL query.

  • $sql – String, SQL query to run.
  • $data – Array, data to feed into query.
$this->core->query(
  "DELETE FROM `users` WHERE `id`=?", [$id]
);
fetchAll($sql, $data, $key) Fetch multiple rows of data.

  • $sql – String, SQL query to run.
  • $data – Array, data to feed into query.
  • $key – String, use this column as the key of the array. Optional.
$users = $this->core->fetchAll(
  "SELECT * FROM `users` WHERE `age`<?",
  [30], "user_id"
);
fetch($sql, $data) Fetch a single row of data.

  • $sql – String, SQL query to run.
  • $data – Array, data to feed into query.
$user = $this->core->fetch(
  "SELECT * FROM `users` WHERE `id`=?", [$id]
);
insert($table, $fields, $data, $replace) Insert or replace data into the specified database table.

  • $table – String, target table.
  • $fields – Array, name of fields.
  • $data – Array, data to feed into query.
  • $replace – Boolean, replace instead of insert?
$this->core->insert("users", 
  ["user_email", "user_password"]
  ["john@doe.com", "123", "jane@doe.com", "456"]
);
update($table, $fields, $where, $data) Run update query.

  • $table – String, target table.
  • $fields – Array, name of fields.
  • $where – String, the “WHERE” clause.
  • $data – Array, data to feed into query.
$this->core->update("table",
  ["user_email", "user_pass"],
  "`user_id`=?",
  ["joy@doe.com", "PASSWORD", 1]
);

 

Engine Core
Function Description & Quick Example
extend($module) Loads lib/$module.php, and extends it to $_CORE->$module = new $module();

  • $module – String, module to load.
respond($status, $message, $more, $http) Formats and outputs a standard JSON encoded string.

  • $status – Boolean, 1, 0, or invent your own set of status code.
  • $message – String, system message.
  • $more – Additional data, if any.
  • $http – Optional, also serve this HTTP status code.

Leave a Comment

Your email address will not be published. Required fields are marked *