$app->configureMode('development', function () use($app) { $app->config(array('debug' => true)); }); $app->configureMode('production', function () use($app) { error_reporting(0); $app->notFound(function () use($app) { $page = new ErrorController(404); $page->render(); }); $app->error(function (Exception $e) use($app) { $app->response()->status(500); if (!$app->request()->isAjax()) { $page = new ErrorController(500); $page->render(); } $app->stop(); if (file_exists(BASE_DIR . '/.gbemail')) { foreach (explode('\\n', file_get_contents(BASE_DIR . '/.gbemail')) as $email) { mail(trim($email), "GetchaBooks Error", get_error_message($e)); } } }); }); $app->hook('slim.before', function () use($app) { global $referrers; $request = $app->request(); define('BASE_URL', $request->getUrl() . $request->getRootUri() . '/'); define('CURRENT_URL', $request->getUrl() . $request->getPath()); define('MOBILE_DEVICE', strpos(strtolower($request->getUserAgent()), 'mobile') !== false); // remove extra slashes $path = $request->getPath();
/** * Test Slim returns 500 response if error thrown within Slim app * * Pre-conditions: * You have initialized a Slim app with a custom Error handler with * an accessible route that calls Slim::error(). * * Post-conditions: * The response status will be 500 */ public function testSlimError() { $this->setExpectedException('Slim_Exception_Stop'); Slim::init(); Slim::get('/', function () { Slim::error(); }); Slim::run(); $this->assertEquals(Slim::response()->status(), 500); }
/** * Test custom error handler uses existing Response object */ public function testErrorHandlerUsesCurrentResponseObject() { $s = new Slim(array('debug' => false)); $s->error(function (Exception $e) use($s) { $r = $s->response(); $r->status(503); $r->write('Foo'); $r['X-Powered-By'] = 'Slim'; echo 'Bar'; }); $s->get('/bar', function () { throw new Exception('Foo'); }); $s->call(); list($status, $header, $body) = $s->response()->finalize(); $this->assertEquals(503, $status); $this->assertEquals('FooBar', $body); $this->assertEquals('Slim', $header['X-Powered-By']); }
ob_start("ob_gzhandler"); header('Content-type: application/json'); require_once "../include_me.php"; require 'Slim/Slim.php'; $app = new Slim(); /** * Set Custom 404 page when an API method is not found */ $app->notFound(function () use($app) { $app->render("api404.html"); }); /** * Set Custom Error Page. No sensetive information should be displayed to the user */ $app->error(function () use($app) { // log error //$app -> render("apiError.html"); }); /** * REST Methods */ $app->get('/teacher/', function () use($teacherModel) { $result = $teacherModel->get(); echo json_encode($result); }); $app->get('/course/', function () use($courseModel) { $result = $courseModel->get(); echo json_encode($result); }); $app->get('/teacherByCourse/:id/', function ($id) use($teacherModel) { $result = $teacherModel->getByCourseId($id); echo json_encode($result);
/** * Test default and custom error handlers * * Pre-conditions: * One app calls the user-defined error handler directly; * One app triggers a generic PHP error; * One app has debugging disabled, throws Exception to invoke error handler indirectly; * * Post-conditions: * All apps' response status is 500; * App with debugging disabled catches ErrorException in user-defined error handler; */ public function testSlimError() { $app1 = new Slim(); $app1->get('/', function () use($app1) { $app1->error(); }); $app2 = new Slim(); $app2->get('/', function () { trigger_error('error'); }); $app3 = new Slim(array('debug' => false)); $app3->error(function ($e) { if ($e instanceof Exception) { echo get_class($e); } }); $app3->get('/', function () { $result = 1 / 0; }); ob_start(); $app1->run(); $app1Out = ob_get_clean(); ob_start(); $app2->run(); $app2Out = ob_get_clean(); ob_start(); $app3->run(); $app3Out = ob_get_clean(); $this->assertEquals(500, $app1->response()->status()); $this->assertEquals(500, $app2->response()->status()); $this->assertEquals('ErrorException', $app3Out); }
if (!isset($post['of']) || trim($post['of']) == "" || $post['of'] == 0 || $post['of'] == "0" || !is_numeric($post['of'])) { $of = "?"; } else { $of = $post['of']; } if ($teams == "?" && $of == "?") { $params['teams'] = App::teamsForEvent($id, "2", "?"); } else { $params['teams'] = App::teamsForEvent($id, $teams, $of); } } } } $params['id'] = $id; $params['errors'] = array(); $app->render('app.tpl', $params); })->via('GET', 'POST'); $app->get('/error/(:code)/', function ($code) use($app) { if (isset($code) && $code != null) { $app->render('error.tpl', array('code' => $code)); } else { $app->render('error.tpl'); } }); $app->notFound(function () use($app) { $app->redirect("/error/404/"); }); $app->error(function (Exception $e) use($app) { $app->notFound(); }); $app->run();
/** * Test error handler receives Exception as argument * * Pre-conditions: * Custom error handler defined; * Invoked app route throws Exception; * * Post-conditions: * Response status is 500; * Error handler's argument is the thrown Exception */ public function testErrorHandlerReceivesException() { $this->expectOutputString('ErrorException'); $app = new Slim(array('debug' => false)); $app->error(function ($e) { if ($e instanceof Exception) { echo get_class($e); } }); $app->get('/', function () { $result = 1 / 0; }); $app->run(); $this->assertEquals(500, $app->response()->status()); }
$app->get('/check', function () use($app) { $url = trim($app->request()->params('url')); if ($url === 'random') { $url = get_random_url(); } // Normalize URL to include "http://" prefix. $url2 = parse_url($url); if (!isset($url2['scheme'])) { $url = "http://{$url2['path']}"; } $data['url'] = $url; $refresh = $app->request()->params('refresh') === '1' ? true : false; $http = new Http(); list($header, $body) = $http->get($url, $refresh); if (!$header) { $app->error(new Exception($body)); } $xml_data = array('Server Information' => 'servers.xml', 'Content Delivery Network' => 'cdn.xml', 'Advertising' => 'ads.xml', 'Analytics and Tracking' => 'trackings.xml', 'CMS' => 'cms.xml', 'Web Portals' => 'portals.xml', 'CSS Frameworks' => 'css.xml', 'Chart Libraries' => 'charts.xml', 'Frameworks' => 'frameworks.xml', 'Javascript Libraries' => 'js.xml', 'Javascript Frameworks' => 'jsframeworks.xml', 'Widgets' => 'widgets.xml', 'Audio/Video' => 'av.xml', 'Aggregation' => 'aggregation.xml', 'Payments' => 'payments.xml', 'Document Info' => 'document.xml', 'Meta Tags' => 'meta.xml'); $site = new Site($header, $body); $results = array(); $mt = microtime(true); foreach ($xml_data as $title => $xml) { $result = $site->analyze(DATA_DIR . $xml); if (!empty($result)) { $results[$title] = $result; } } $data['analysis_time'] = sprintf("%.02f", microtime(true) - $mt); $data['total_time'] = sprintf("%.02f", $http->total_time); $data['speed_download'] = format_filesize($http->speed_download); $data['size_download_gzip'] = format_filesize($http->size_download_gzip);
<?php /* * RESTFul API for an employee directory application. Sandbox for offline-sync experimentation. Maintain a session-based * and updatable employee list that mimics a real-life database-powered backend while enabling multiple users to * experiment with CRUD operations on their isolated data set without compromising the integrity of a central database. */ require 'Slim/Slim.php'; session_start(); if (!isset($_SESSION['employees'])) { $_SESSION['employees'] = array((object) array("id" => 1, "firstName" => "John", "lastName" => "Smith", "title" => "CEO", "officePhone" => "617-321-4567", "lastModified" => "2008-01-01 00:00:00", "deleted" => false), (object) array("id" => 2, "firstName" => "Lisa", "lastName" => "Taylor", "title" => "VP of Marketing", "officePhone" => "617-522-5588", "lastModified" => "2011-06-01 01:00:00", "deleted" => false), (object) array("id" => 3, "firstName" => "James", "lastName" => "Jones", "title" => "VP of Sales", "officePhone" => "617-589-9977", "lastModified" => "2009-08-01 16:30:24", "deleted" => false), (object) array("id" => 4, "firstName" => "Paul", "lastName" => "Wong", "title" => "VP of Engineering", "officePhone" => "617-245-9785", "lastModified" => "2012-05-01 08:22:10", "deleted" => false), (object) array("id" => 5, "firstName" => "Alice", "lastName" => "King", "title" => "Architect", "officePhone" => "617-744-1177", "lastModified" => "2012-02-10 22:58:37", "deleted" => false), (object) array("id" => 6, "firstName" => "Jen", "lastName" => "Brown", "title" => "Software Engineer", "officePhone" => "617-568-9863", "lastModified" => "2010-01-15 11:17:45", "deleted" => false), (object) array("id" => 7, "firstName" => "Amy", "lastName" => "Garcia", "title" => "Software Engineer", "officePhone" => "617-327-9966", "lastModified" => "2011-07-03 14:24:50", "deleted" => false), (object) array("id" => 8, "firstName" => "Jack", "lastName" => "Green", "title" => "Software Engineer", "officePhone" => "617-565-9966", "lastModified" => "2012-04-28 10:25:56", "deleted" => false)); } $app = new Slim(array('debug' => false)); $app->error(function (Exception $e) use($app) { echo $e->getMessage(); }); $app->get('/employees', 'getEmployees'); $app->post('/employees', 'addEmployee'); $app->put('/employees/:id', 'updateEmployee'); $app->delete('/employees/:id', 'deleteEmployee'); $app->run(); function getEmployees() { if (isset($_GET['modifiedSince'])) { getModifiedEmployees($_GET['modifiedSince']); return; } $employees = $_SESSION['employees']; $result = array(); foreach ($employees as $employee) { if (!$employee->deleted) {