DEV Community


Here's how I make app fast yet flexible to extend. Suitable for those who want to ship app fast (Suitable for any langs)

ricardosawir profile image Ricardo Sawir ・2 min read

I wrote SingleFile App

This is how I built apps fast in a single file. You're free to modify how it should be done.

Some examples I built with this file

Follow me on Twitter for feedbacks or live updates

See the repository here:

// 1. uncomment if you want to turn on your error
// error_reporting(E_ALL);

// 2. define your ABSPATH here. It's your choice you want to place this in root or in a 'public' folder
// this assume you put index.php in your public folder
define('ABSPATH', dirname(__DIR__));

// ucomment if you use composer/3rd party vendor
// include_once join(DIRECTORY_SEPARATOR, [ABSPATH, 'vendor', 'autoload.php']);

// you can include other files here if you want

 * Main App process happening here
 * @author Ricardo <>
 * @author Follow me on Twitter for feedback or live updates https: //
 * @return void
function run_app()
    //you can also initialize session here
    $arr_route = (explode('/', ltrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/')));
    if ($_SERVER['REQUEST_METHOD'] == 'GET') {
        if ($arr_route[0] == '') {
            // handle index here
        //examples you can use to access other route
        elseif ($arr_route[0] == 'about') {
            // handle /about

        } elseif ($arr_route[0] == 'contact') {
//handle /contact
// by the way, you can also add route for /api here
        elseif ($arr_route[0] == 'api') {

        // we need to handle assets here, else you cannot access assets
        // (I assume you use folder named 'assets')
        elseif ($arr_route[0] == 'assets') {
        } else {
            //or die ('404');
    } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
// handle your post request here
    // if you want to include other request method like DELETE/PUT/PATH

function controller_get_404()
    // handle view
    echo render_header();
    echo render_footer();

function controller_get_assets()
    $arr_route = explode('/', ltrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'));
    unset($arr_route[0], $arr_route[1]);
    $str_path = join(DIRECTORY_SEPARATOR, $arr_route);
    $name     = join(DIRECTORY_SEPARATOR, [ABSPATH, 'public', 'assets', $str_path]);
    $fp       = fopen($name, 'rb');
// send the right headers
    header("Content-Type: image/png");
    header("Content-Length: " . filesize($name));
// dump the picture and stop the script

function controller_get_index()
// handle view
    echo render_header();
    // I name this render_index just to follow the name convention. If your controller is controller_get_random_name, then I suggest you name render_random_name. Just to make things more easy to organize later.
    echo render_index();
    echo render_footer();


function render_index()
<?php return ob_get_clean();

function render_header()
<!DOCTYPE html>
<html lang="en">

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <?php return ob_get_clean();

function render_footer()

<?php return ob_get_clean();
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Editor guide