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") { $config = array('service-name' => $serviceName, 'longUrl' => $longUrl, 'apiKey' => $key[$serviceName]["apiKey"], 'login' => $key[$serviceName]["login"]); } else { $config = array('service-name' => $serviceName, 'longUrl' => $longUrl); } } $bundle = new \peter\components\serviceBundle\serviceBundle($config); echo json_encode($bundle->sendReq()); } else { echo "Oops ! no data input"; } }); $router->set404(function () { header('HTTP/1.1 404 Not Found'); // ... do something special here echo file_get_contents("404.html"); }); $router->run();
public function test404() { // Create Router $router = new \Bramus\Router\Router(); $router->get('/', function () { echo 'home'; }); $router->set404(function () { echo 'route not found'; }); // Test the /hello route ob_start(); $_SERVER['REQUEST_URI'] = '/'; $router->run(); $this->assertEquals('home', ob_get_contents()); // Test the /hello/bramus route ob_clean(); $_SERVER['REQUEST_URI'] = '/foo'; $router->run(); $this->assertEquals('route not found', ob_get_contents()); // Cleanup ob_end_clean(); }
<?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();
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();
global $events; $events['auth0']['method'] = 'public'; $events['auth0']['authorized'] = true; $events['auth0']['api'] = true; $events['auth0']['user'] = true; }); /** * These is the private API save Forplay content and see the log. */ $router->match('POST|GET', '(log.*|save.*|imgs.*|sitemap.*)', function () { global $events; $events['auth0']['method'] = 'secure'; $events['auth0']['authorized'] = true; $events['auth0']['api'] = true; $events['auth0']['user'] = true; }); /** * If someone tries to access unknown API. */ $router->set404(function () { global $events; header('HTTP/1.1 404 Not Found'); $events['auth0']['method'] = false; $events['auth0']['authorized'] = true; $events['auth0']['api'] = false; $events['auth0']['user'] = true; }); /** * Run the router. */ $router->run();
$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); }); } /** * Redirect on unmatched routes. */ $router->set404(function () { header('HTTP/1.1 301 Moved Permanently'); header('Location: /'); echo '404, route not found!'; }); /** * Dispatch the request. */ $router->run();
$filename = __DIR__ . preg_replace('#(\\?.*)$#', '', $_SERVER['REQUEST_URI']); if (php_sapi_name() === 'cli-server' && is_file($filename)) { return false; } // Include the Router class require_once __DIR__ . '/core/Router.php'; // Include configuration and models require_once __DIR__ . '/includes/config.php'; require_once __DIR__ . '/core/Model.php'; require_once __DIR__ . '/model/Post.php'; require_once __DIR__ . '/model/FeedSource.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('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);