Example #1
0
function route($method, $pattern, $callback = null)
{
    // callback map by request type
    static $route_map = array('GET' => array(), 'POST' => array(), 'DELETE' => array());
    $method = strtoupper($method);
    if (!in_array($method, array('GET', 'POST', 'DELETE'))) {
        error(500, 'Only GET, DELETE and POST are supported');
    }
    // a callback was passed, so we create a route defiition
    if ($callback !== null) {
        // create a route entry for this pattern
        $route_map[$method][$pattern] = array('xp' => route_to_regex($pattern), 'cb' => $callback);
    } else {
        // callback is null, so this is a route invokation. look up the callback.
        foreach ($route_map[$method] as $pat => $obj) {
            // if the requested uri ($pat) has a matching route, let's invoke the cb
            if (!preg_match($obj['xp'], $pattern, $vals)) {
                continue;
            }
            // call middleware
            middleware($pattern);
            // construct the params for the callback
            array_shift($vals);
            preg_match_all('@:([\\w]+)@', $pat, $keys, PREG_PATTERN_ORDER);
            $keys = array_shift($keys);
            $argv = array();
            foreach ($keys as $index => $id) {
                $id = substr($id, 1);
                if (isset($vals[$id])) {
                    array_push($argv, trim(urldecode($vals[$id])));
                }
            }
            // call filters if we have symbols
            if (count($keys)) {
                filter(array_values($keys), $vals);
            }
            // if cb found, invoke it
            if (is_callable($obj['cb'])) {
                call_user_func_array($obj['cb'], $argv);
            }
            // leave after first match
            break;
        }
    }
}
Example #2
0
 public function get($k, $d = null)
 {
     return middleware($k, null, $d);
 }
Example #3
0
 private function analyze($routes, $quit = true)
 {
     $found = 0;
     $uri = $this->getUri();
     foreach ($routes as $route) {
         if (preg_match_all('#^' . $route['pattern'] . '$#', $uri, $matches, PREG_OFFSET_CAPTURE)) {
             $matches = array_slice($matches, 1);
             $params = array_map(function ($match, $index) use($matches) {
                 if (isset($matches[$index + 1]) && isset($matches[$index + 1][0]) && is_array($matches[$index + 1][0])) {
                     return trim(substr($match[0][0], 0, $matches[$index + 1][0][1] - $match[0][1]), '/');
                 } else {
                     return isset($match[0][0]) ? trim($match[0][0], '/') : null;
                 }
             }, $matches, array_keys($matches));
             if ($quit) {
                 $middleware = isAke($route, 'middleware', null);
                 if ($middleware) {
                     $middleware = middleware($middleware);
                     if (is_callable($middleware)) {
                         $middleware($_REQUEST);
                     }
                 }
                 $this->route = call_user_func_array($route['callback'], $params);
             } else {
                 call_user_func_array($route['callback'], $params);
             }
             $found++;
             if ($quit) {
                 break;
             }
         }
     }
     return $found;
 }
Example #4
0
});
get("/splat/*", function ($params) {
    echo $params['splat'][0];
});
get("/captures/(.*?)", function ($params) {
    echo $params['captures'][0];
});
get("/halt", function () {
    halt(404, 'Go away', array('Content-Type' => 'text/plain'));
});
not_found(function () {
    echo "This file wasn't found, yo!";
});
class Middleware
{
    function call($output)
    {
        return array(200, array(), 'asdf');
    }
}
class WrapMiddleware
{
    function call($output)
    {
        return array(200, array(), "Before {$output['2']} After");
    }
}
get("/middleware", function () {
    middleware('Middleware');
    middleware('WrapMiddleware');
});
Example #5
0
<?php

return [['GET', '/', ['App\\Http\\Controllers\\WelcomeController', 'index'], middleware()], ['GET', '/hello/:name', ['App\\Http\\Controllers\\WelcomeController', 'hello'], middleware()]];