Beispiel #1
0
 private function initRouter()
 {
     $this->router = new RouteCollector();
     $this->router->get('/cmd{id:\\d+}', function ($id) {
         $controller = new Controller($this);
         $content = $controller->actionIncrement($id);
         return $content;
     });
     $this->router->get('/cmd', function () {
         $controller = new Controller($this);
         $this->responseContentType = 'application/json';
         $content = $controller->actionSummary();
         return $content;
     });
     $this->router->post('/cmd{id:\\d+}', function ($id) {
         $this->request->on('data', function ($data) use($id) {
             parse_str($data, $data);
             $controller = new Controller($this);
             $content = $controller->actionUpdate($id, $data);
             return $content;
         });
     });
 }
Beispiel #2
0
<?php

use Phroute\Phroute\RouteCollector;
require __DIR__ . '/config.php';
require __DIR__ . '/./vendor/autoload.php';
$router = new RouteCollector();
$router->get('/{id}', function ($id) {
    $shorts_path = __DIR__ . '/shorts/' . $id . '.php';
    if (file_exists($shorts_path)) {
        header("Status: 200 OK", false, 200);
        require $shorts_path;
        die;
    } else {
        header('Bad Request', true, 404);
        header('Content-Type: application/json');
        echo json_encode(array('error' => "Le shortcut {$id} n'existe pas."));
    }
});
// $router->get('/cut/{url:(http\:\/\/)?[a-zA-Z0-9+_\-\.]+\.[a-zA-Z]{2,3}}', function($uri){
//   require(__DIR__ . '/shorten.php');
// });
// $router->get('/cut/{url}/{shortid}', function($uri, $short_id){
//   require(__DIR__ . '/shorten.php');
// });
$router->any('/', function () {
    return 'Hello :) ';
});
$dispatcher = new Phroute\Phroute\Dispatcher($router->getData());
$response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
echo $response;
Beispiel #3
0
<?php

include __DIR__ . '/../vendor/autoload.php';
use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\Dispatcher;
$collector = new RouteCollector();
$USER_SESSION = false;
$collector->filter('auth', function () use(&$USER_SESSION) {
    if (!$USER_SESSION) {
        return "Nope! Must be authenticated";
    }
});
$collector->group(array('before' => 'auth'), function (RouteCollector $collector) {
    $collector->get('/', function () {
        return 'Hurrah! Home Page';
    });
});
$dispatcher = new Dispatcher($collector->getData());
echo $dispatcher->dispatch('GET', '/'), "\n";
// Nope! Must be authenticated
$USER_SESSION = true;
echo $dispatcher->dispatch('GET', '/'), "\n";
// Hurrah! Home Page
Beispiel #4
0
<?php

/*
|--------------------------------------------------------------------------
| ROUTES(路由)映射表
|--------------------------------------------------------------------------
|
| 目前想法是优先匹配配置文件,找不到配置文件自动GET请求自动匹配控制器方法,
| 例如/user 自动匹配UserController@index
|
*/
use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\Dispatcher;
$collector = new RouteCollector();
$collector->get('/', function () {
    echo 'Home Page';
});
$collector->post('products', function () {
    return 'Create Product';
});
$collector->put('items/{id}', function ($id) {
    return 'Amend Item ' . $id;
});
$dispatcher = new Dispatcher($collector->getData());
return $dispatcher;
//echo $dispatcher->dispatch('GET', '/'), "\n";   // Home Page
//echo $dispatcher->dispatch('POST', '/products'), "\n"; // Create Product
//echo $dispatcher->dispatch('PUT', '/items/123'), "\n"; // Amend Item 123
Beispiel #5
0
<?php

/**
 * @file
 * Implement the router so we can show some content.
 */
use Phroute\Phroute\RouteCollector;
// Create the router.
$router = new RouteCollector();
/**
 * ----------------------------------------------------------------------------
 * Application Routes
 * ----------------------------------------------------------------------------
 * Define routes for the application to use here. These routes can be mapped to
 * controllers or use simple named routes with anonymous callback functions.
 *
 * Examples:
 *
 * // Basic anonymous function route.
 * $router->get('/basic', function () {
 *     return 'This is a basic route';
 * });
 *
 * // Controller based route.
 * $router->get('/', ['NotAFramework\App\Controllers\Example', 'show']);
 *
 * @see https://github.com/mrjgreen/phroute
 */
$router->get('/', ['NotAFramework\\App\\Controllers\\HelloWorldController', 'show']);
$router->get('/json', ['NotAFramework\\App\\Controllers\\HelloWorldController', 'json']);
Beispiel #6
0
 public function getDispatcher()
 {
     static $dispatcher;
     if ($dispatcher) {
         return $dispatcher;
     }
     $routes = (include $this->config['config'] . '/routes.php');
     //TODO: Check if routes have been cached
     if (true) {
         $collector = new RouteCollector();
         foreach ($routes as $left => $right) {
             // $left is something like 'GET /'
             // $right is a callable
             $left = explode(" ", $left);
             if (sizeof($left) === 2) {
                 switch ($left[0]) {
                     case 'GET':
                         $collector->get($left[1], $right);
                         break;
                     case 'POST':
                         $collector->post($left[1], $right);
                         break;
                     case 'ANY':
                         $collector->any($left[1], $right);
                         break;
                     case 'HEAD':
                         $collector->head($left[1], $right);
                         break;
                     case 'OPTIONS':
                         $collector->options($left[1], $right);
                         break;
                     case 'DELETE':
                         $collector->delete($left[1], $right);
                         break;
                     default:
                         throw new ConfigException("Don't understand the HTTP method '" . $left[0] . "' in routes.php.");
                 }
             }
         }
         $routeData = $collector->getData();
     }
     return $dispatcher = new Dispatcher($routeData, $this->getHandlerResolver());
 }
<?php

include __DIR__ . '/../vendor/autoload.php';
use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\Dispatcher;
$collector = new RouteCollector();
$collector->filter('auth', function () {
    return "Nope!";
});
$collector->group(array('prefix' => 'admin'), function (RouteCollector $collector) {
    $collector->group(['before' => 'auth'], function (RouteCollector $collector) {
        $collector->get('pages', function () {
            return 'page management';
        });
        $collector->get('products', function () {
            return 'product management';
        });
    });
    // Not inside auth group
    $collector->get('orders', function () {
        return 'Order management';
    });
});
$dispatcher = new Dispatcher($collector->getData());
echo $dispatcher->dispatch('GET', '/admin/pages'), "\n";
// Nope!
echo $dispatcher->dispatch('GET', '/admin/products'), "\n";
// Nope!
echo $dispatcher->dispatch('GET', '/admin/orders'), "\n";
// order management
Beispiel #8
-1
<?php

require_once '../vendor/autoload.php';
use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\Dispatcher;
$loader = new Twig_Loader_Filesystem('./views');
$twig = new Twig_Environment($loader, ['debug' => true, 'cache' => './compilation_cache']);
$collector = new RouteCollector();
$collector->get('/', function () {
    global $twig;
    return $twig->render('home.twig', ['name' => 'Fabien']);
});
$collector->get('/about', function () {
    global $twig;
    return $twig->render('about.twig', ['name' => 'Fabien']);
});
$dispatcher = new Dispatcher($collector->getData());
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = rawurldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
echo $dispatcher->dispatch($httpMethod, $uri), "\n";