Exemplo n.º 1
0
<?php

require_once __DIR__ . '/vendor/autoload.php';
$router = new \Bramus\Router\Router();
$router->get('/', function () {
    // do something clever
    if (file_exists("index.html")) {
        echo file_get_contents("index.html");
    } else {
        echo "index file not found.";
    }
});
$router->get('/js/index.js', function () {
    // do something clever
    if (file_exists("index.js")) {
        echo file_get_contents("index.js");
    } else {
        echo "index.js file not found.";
    }
    return $response;
});
$router->post('/post/url', function () {
    $serviceName = filter_input(INPUT_POST, "sel-service");
    $longUrl = filter_input(INPUT_POST, "longUrl");
    if (isset($serviceName) && isset($longUrl)) {
        $key = file_get_contents("auth/key.txt");
        $key = json_decode($key, true);
        if ($serviceName === "goo.gl") {
            $config = array('service-name' => $serviceName, 'longUrl' => $longUrl, 'apiKey' => $key[$serviceName]);
        } else {
            if ($serviceName === "bit.ly") {
Exemplo n.º 2
0
 public function testSubrouteMouting()
 {
     // Create Router
     $router = new \Bramus\Router\Router();
     $router->mount('/movies', function () use($router) {
         $router->get('/', function () {
             echo 'overview';
         });
         $router->get('/(\\d+)', function ($id) {
             echo htmlentities($id);
         });
     });
     // Test the /movies route
     ob_start();
     $_SERVER['REQUEST_URI'] = '/movies';
     $router->run();
     $this->assertEquals('overview', ob_get_contents());
     // Test the /hello/bramus route
     ob_clean();
     $_SERVER['REQUEST_URI'] = '/movies/1';
     $router->run();
     $this->assertEquals('1', ob_get_contents());
     // Cleanup
     ob_end_clean();
 }
Exemplo n.º 3
0
<?php

// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';
require 'controller/controller.php';
// Create Router instance
$router = new \Bramus\Router\Router();
$router->before('GET', '/.*', function () {
    header('X-Powered-By: router');
});
$router->get('/', function () {
    echo "Welcome to beautyUniversity JSON api";
});
$router->get('/v1/school/(\\w+)/analytic/(\\w+)', function ($name, $bool) {
    header('Content-Type: application/json; charset=utf-8');
    ob_start("ob_gzhandler");
    $req = htmlentities($name);
    $check = htmlentities($bool);
    $controller = new myController($req);
    if ($check === "true") {
        echo $controller->indexAction("colleges_" . $req);
    }
    if ($check === "false") {
        echo $controller->indexAction("school_" . $req);
    }
});
$router->set404(function () {
    header('HTTP/1.1 404 Not Found');
    echo "invalid request url";
});
$router->run();
Exemplo n.º 4
0
        header('HTTP/1.0 401 Unauthorized');
        echo "No token provided.";
        exit;
    }
    $authorizationHeader = $requestHeaders['Authorization'];
    if ($authorizationHeader == null) {
        header('HTTP/1.0 401 Unauthorized');
        echo "No authorization header sent";
        exit;
    }
    $token = str_replace('Bearer ', '', $authorizationHeader);
    try {
        $app->setCurrentToken($token);
    } catch (\Auth0\SDK\Exception\CoreException $e) {
        header('HTTP/1.0 401 Unauthorized');
        echo "Invalid token";
        exit;
    }
});
$router->get('/ping', function () use($app) {
    echo json_encode($app->publicPing());
});
$router->get('/secured/ping', function () use($app) {
    echo json_encode($app->privatePing());
});
$router->set404(function () {
    header('HTTP/1.1 404 Not Found');
    echo "Page not found";
});
// Run the Router
$router->run();
Exemplo n.º 5
0
$router = new \Bramus\Router\Router();
// Custom 404 Handler
$router->set404(function () {
    header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
    echo '404, route not found!';
});
// Before Router Middleware
$router->before('GET', '/.*', function () {
    header('Content-Type: application/json');
});
// // Static route: / (homepage)
// $router->get('/', function () {
// 	echo '';
// });
$router->mount('/post', function () use($router) {
    // Route: /posts (fetch all posts)
    $router->get('/', function () {
        $postModel = new Post();
        $posts = $postModel->getAllPosts();
        echo json_encode($posts);
    });
    // Route: /post/id (fetch a single post)
    $router->get('/(\\d+)', function ($id) {
        $postModel = new Post();
        $post = $postModel->getPost($id);
        echo json_encode($post);
    });
});
// Thunderbirds are go!
$router->run();
// EOF
Exemplo n.º 6
0
// @note: it's recommended to just use the composer autoloader when working with other packages too
require_once __DIR__ . '/../src/Bramus/Router/Router.php';
// Create a Router
$router = new \Bramus\Router\Router();
// Custom 404 Handler
$router->set404(function () {
    header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
    echo '404, route not found!';
});
// Before Router Middleware
$router->before('GET', '/.*', function () {
    header('X-Powered-By: bramus/router');
});
// Static route: / (homepage)
$router->get('/', function () {
    echo '<h1>bramus/router</h1><p>Try these routes:<p><ul><li>/hello/<em>name</em></li><li>/blog</li><li>/blog/<em>year</em></li><li>/blog/<em>year</em>/<em>month</em></li><li>/blog/<em>year</em>/<em>month</em>/<em>day</em></li><li>/movies</li><li>/movies/<em>id</em></li></ul>';
});
// Static route: /hello
$router->get('/hello', function () {
    echo '<h1>bramus/router</h1><p>Visit <code>/hello/<em>name</em></code> to get your Hello World mojo on!</p>';
});
// Dynamic route: /hello/name
$router->get('/hello/(\\w+)', function ($name) {
    echo 'Hello ' . htmlentities($name);
});
// Dynamic route: /ohai/name/in/parts
$router->get('/ohai/(.*)', function ($url) {
    echo 'Ohai ' . htmlentities($url);
});
// Dynamic route with (successive) optional subpatterns: /blog(/year(/month(/day(/slug))))
$router->get('/blog(/\\d{4}(/\\d{2}(/\\d{2}(/[a-z0-9_-]+)?)?)?)?', function ($year = null, $month = null, $day = null, $slug = null) {
Exemplo n.º 7
0
require 'vendor/autoload.php';
/**
 * Let the provisioner automatically intervene and reconfigure Apache. This only
 * happens when users try to access a domain that Apache isn't aware off yet and
 * the provisioner will then - just in time - install vhost files and reload.
 */
\LXC\VirtualHost\ApacheProvisioner::check('root', 'root', 'templates/rebuilding.html', 'templates/vhost.conf');
/**
 * Configure the router.
 */
$router = new \Bramus\Router\Router();
$t = new \h2o();
# ROUTE /: index listing.
$router->get('/', function () use($t) {
    $t->loadTemplate('templates/listing.html');
    print $t->render(array('lxc' => \LXC\Container\Variables::get(), 'vhosts' => \LXC\VirtualHost\Listing::get(), 'hostsoutdated' => \LXC\VirtualHost\Listing::are_hosts_outdated(), 'logfiles' => \LXC\Logging\Files::get(), 'hostname' => gethostname()));
});
# ROUTE /php: PHP information.
$router->get('/php', function () {
    phpinfo();
});
# ROUTE /$LOGFILE: tail -f style log viewer.
foreach (\LXC\Logging\Files::get() as $logfile) {
    $path = '/' . $logfile->name;
    $router->get($path, function () use($t, $logfile) {
        $t->loadTemplate('templates/logtail.html');
        print $t->render(array('file' => $logfile, 'lxc' => \LXC\Container\Variables::get(), 'hostname' => gethostname()));
    });
    $router->get($path . '/(\\d+)', function ($from_line) use($t, $logfile) {
        $logfile->sendPayload((int) $from_line);
    });