Skip to the content.

Files

These are the setup files, which you should include to get the best experience out of the framework.

index.php

Starts the dispatcher and sets up the services, which are defined in the services/ folder.

<?php
require "./vendor/autoload.php";
require "./autoload.php";

use Julianschmuckli\Restwork\Dispatcher;

$dispatcher = new Dispatcher();
$dispatcher->init();

autoload.php

The autoload.php file is not to be confused with the vendor/autoload.php, generated by the composer. This is a standalone implementation by the framework, to include all the files of the 4 object types (dao/, entities/, routes/ and services/).

<?php
require_once "./vendor/autoload.php";
/**
 * Auto import all the custom files, which are not part of the _framework.
 */

define("CUSTOM_DIR", ".");

/**
 * Import all services classes
 */
$services = rsearch(CUSTOM_DIR . '/services', '/.*\.php/');
foreach ($services as $service) {
    require_once($service);   
}

/**
 * Import all entites classes
 */
$entities = rsearch(CUSTOM_DIR . '/entities', '/.*\.php/');
foreach ($entities as $entity) {
    require_once($entity);   
}

/**
 * Import all DAO (Data access object) classes
 */
$daos = rsearch(CUSTOM_DIR . '/dao', '/.*\.php/');
foreach ($daos as $dao) {
    require_once($dao);   
}

/**
 * Import all route classes
 */
$routes = rsearch(CUSTOM_DIR . '/routes', '/.*\.php/');
foreach ($routes as $route) {
    require_once($route);   
}

// $regPattern should be using regular expression
// Source: https://stackoverflow.com/questions/17160696/php-glob-scan-in-subfolders-for-a-file
function rsearch($folder, $regPattern) {
    try {
        $dir = new RecursiveDirectoryIterator($folder);
        $ite = new RecursiveIteratorIterator($dir);
        $files = new RegexIterator($ite, $regPattern, RegexIterator::GET_MATCH);
        $fileList = array();
        foreach($files as $file) {
            $fileList = array_merge($fileList, $file);
        }
        return $fileList;
    } catch (Exception $e) {
        return [];
    }
}

.htaccess

This file is specific for Apache web server and rewrites the URLs of the framework.

If you are using another web server like nginx, please reproduce this code. And if you would like, create a pull request with this new configuration file.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?service=$1 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?service=$1&route=$2 [L,QSA]