/** * @param string $repositoryName * @param string $packageName * @return \Composer\Package\PackageInterface */ public function getPackage($repositoryName, $packageName) { $repository = $this->getRepository($repositoryName); $packages = $repository->findPackages($packageName); if (!$packages) { $this->app->redirect(ROOTURL . '/' . $repositoryName); } return array_shift($packages); }
function authuser($role = 'member') { $user = User::fetchFromDatabaseSomehow(); if ($user->belongsToRole($role) === false) { Slim::flash('error', 'Login required'); Slim::redirect('/login'); } }
ActiveRecord\Config::initialize(function ($cfg) { $cfg->set_model_directory('models'); $cfg->set_connections(array('development' => 'mysql://*****:*****@127.0.0.1/test')); }); $app = new Slim(); $app->get('/', function () use($app) { $data['tasks'] = Task::find('all'); $app->render('task/index.php', $data); })->name('tasks'); $app->post('/task/new/', function () use($app) { $task = new Task(); $task->name = "My New Task"; $task->done = 0; $task->save(); if ($task->id > 0) { $app->redirect($app->urlFor('tasks')); } })->name('task_new'); $app->get('/task/:id/edit', function ($id) use($app) { $data['task'] = Task::find($id); $app->render('task/edit.php', $data); })->name('task_edit'); $app->post('/task/:id/edit', function ($id) use($app) { $task = Task::find($id); $task->name = $app->request()->post('name'); $task->done = $app->request()->post('done') === '1' ? 1 : 0; $task->save(); if ($task->id > 0) { $app->redirect($app->urlFor('tasks')); } })->name('task_edit_post');
/** * Test Slim::redirect fails if status is not 301 or 302 * * Pre-conditions: * You have initialized a Slim application with an accessible route. * The route attempts to redirect with an invalid HTTP status code. * * Post-conditions: * Slim throws an InvalidArgumentException which is caught by * the Slim::run dispatch loop, and the Response status is set to 500. */ public function testSlimRedirectFails() { Slim::init(); Slim::get('/', function () { Slim::redirect('/foo', 400); }); Slim::run(); $this->assertEquals(Slim::response()->status(), 500); }
/** * Test redirect sets status and header */ public function testRedirect() { $s = new Slim(); $s->get('/bar', function () use($s) { echo "Foo"; //<-- Should not be in response body! $s->redirect('/somewhere/else', 303); }); $s->call(); list($status, $header, $body) = $s->response()->finalize(); $this->assertEquals(303, $status); $this->assertEquals('/somewhere/else', $header['Location']); $this->assertEquals('/somewhere/else', $body); }
<?php require_once 'Views/Twig/Autoloader.php'; Twig_Autoloader::register(); require 'Slim/Slim.php'; require 'Views/TwigView.php'; require 'lib/database.php'; $app = new Slim(array('view' => 'TwigView', 'http.version' => '1.0')); $app->get('/', function () use($app) { $app->redirect('wishlist'); }); $app->get('/wishlist(/:order)', function ($order = 'year') use($app) { $app->etag('wishlist'); $albums = Database::getAlbums('wishlist', $order); $data = array('albums' => $albums, 'page' => 'wishlist', 'order' => $order); $app->render('index.html', $data); }); $app->get('/coleccao(/:order)', function ($order = 'year') use($app) { $app->etag('coleccao'); $albums = Database::getAlbums('collection', $order); $data = array('albums' => $albums, 'page' => 'coleccao', 'order' => $order); $app->render('index.html', $data); }); $app->run();
// render the main page $blog->render('index.html', array('posts' => $posts_limit, 'page_current' => 1, 'page_count' => $pages_number)); }); // about page $blog->get('/about', function () use($blog) { // render the about page $blog->render('about.html'); }); // projects page $blog->get('/projects', function () use($blog) { // render the blog page $blog->render('projects.html'); }); // redirect blog/page1 to root index $blog->get('/blog/1', function () use($blog) { $blog->redirect('/'); }); // blog pagination $blog->get('/blog/:number', function ($number) use($blog, $getMarkdownPosts) { // get our posts $posts = $getMarkdownPosts($blog->config('posts.path')); // determine the start position of the array $previous_page = $number - 1; $start_index = $previous_page * $blog->config('pagination'); // limit our posts to pagination $posts_limit = array_slice($posts, $start_index, $blog->config('pagination')); // get the total number of posts $posts_count = count($posts); // calculate the number of page $pages_number = ceil($posts_count / $blog->config('pagination')); // if the requested page is too high, 401
$cl_scraper->initialize($_POST['include']); echo $cl_scraper; exit; } } catch (Exception $e) { echo $e->getMessage(); } }); $app->get('/site/:site', function ($site) use($app, $sites) { $init = true; $loadConfiguration = "findjobs.locations.xml"; if (isset($sites[$site])) { $loadConfiguration = "{$site}.locations.xml"; $sites[$site] = true; } else { $app->redirect('/'); } try { require 'lib/CraigListScraper.class.php'; $cl_scraper = new CraigListScraper("sites/{$loadConfiguration}"); $title = $cl_scraper->getInfo()->title; require 'templates/index.php'; } catch (Exception $e) { echo $e->getMessage(); } }); $app->get('/site/:site/data', function ($site) use($app, $sites) { $init = true; $loadConfiguration = "findjobs.locations.xml"; if (isset($sites[$site])) { $loadConfiguration = "{$site}.locations.xml";
$params['user_name'] = $user->first_name . ' ' . $user->last_name; $params['button'] = "Logout"; } } } else { if ($app->request()->get('error') == 'access_denied') { $params['errors'][] = "Access Denied"; } } } $params['oauth_link'] = App::getOauthLink(); $app->render('app.tpl', $params); })->via('GET', 'POST'); $app->get('/logout/', function () use($app) { App::end(); $app->redirect("/"); }); $app->get('/events/', 'authenticate', function () use($app) { $params = App::start(); $params['title'] = "Events"; $params['page'] = "events"; $params['button'] = "Logout"; $params['errors'] = array(); try { $params['events'] = App::client()->user_list_events(array('event_statuses' => 'live,started'))->events; } catch (Exception $e) { $params['events'] = array(); } $app->render('app.tpl', $params); }); $app->map('/event/:id/(:tool)', 'authenticate', function ($id, $tool = null) use($app) {
return $app->render('admin_home.html', array('articles' => $articles)); }); // Admin Add. $app->get('/admin/add', $authCheck, function () use($app) { return $app->render('admin_input.html', array('action_name' => 'Add', 'action_url' => '/microframework/admin/add')); }); // Admin Add - POST. $app->post('/admin/add', $authCheck, function () use($app) { $article = Model::factory('Article')->create(); $article->title = $app->request()->post('title'); $article->author = $app->request()->post('author'); $article->summary = $app->request()->post('summary'); $article->content = $app->request()->post('content'); $article->timestamp = date('Y-m-d H:i:s'); $article->save(); $app->redirect('/microframework/admin'); }); // Admin Edit. $app->get('/admin/edit/(:id)', $authCheck, function ($id) use($app) { $article = Model::factory('Article')->find_one($id); if (!$article instanceof Article) { $app->notFound(); } return $app->render('admin_input.html', array('action_name' => 'Edit', 'action_url' => '/microframework/admin/edit/' . $id, 'article' => $article)); }); // Admin Edit - POST. $app->post('/admin/edit/(:id)', $authCheck, function ($id) use($app) { $article = Model::factory('Article')->find_one($id); if (!$article instanceof Article) { $app->notFound(); }
* @var Slim */ $app = new Slim(array('templates.path' => TEMPLATE_PATH)); /** * Add a middleware to all routes. Adding commonly accessed variables to the * view. */ $app->add(new puny\helpers\ViewHelper()); /** * This is a Slim middleware route that prevents non logged in visitors to * access that route */ $locked = function () use($app) { return function () use($app) { if (!puny\User::is_logged_in()) { $app->redirect($app->urlFor('login')); } }; }; /** * This is the index page */ $app->get('/', function () use($app) { $posts = puny\Blog::get_posts(5); $app->render('home.php', array('posts' => $posts)); })->name('index'); /** * Show a single post */ $app->get('/blog/:url', function ($url) use($app) { $blog = new puny\Blog('posts/');
/** * Slim Flash with Redirect * * Pre-conditions: * Slim app sets Flash message for next request; * Slim app halts with 302 redirect * * Post-conditions: * Message is persisted to $_SESSION after app is run; */ public function testSlimFlashWithRedirect() { $app = new Slim(); $app->get('/', function () use($app) { $app->flash('info', 'Foo redirect'); $app->redirect('/foo'); }); $app->run(); $this->assertArrayHasKey('info', $_SESSION['flash']); $this->assertEquals('Foo redirect', $_SESSION['flash']['info']); }
// Models require 'models/Article.php'; require 'models/User.php'; // Configuration TwigView::$twigDirectory = __DIR__ . '/Twig/lib/Twig/'; ORM::configure('mysql:host=localhost;dbname=blog'); ORM::configure('username', 'root'); ORM::configure('password', ''); // Start Slim. $app = new Slim(array('view' => new TwigView())); // Blog Homepage. $app->get('/', function () use($app) { $articles = Model::factory('Article')->order_by_desc('timestamp')->find_many(); $isLoginned = User::isLoginned(); if ($isLoginned) { return $app->redirect('/newtest/user/' . $_COOKIE['login']); } else { return $app->redirect('/newtest/feed'); } }); // Blog Feed. $app->get('/feed', function () use($app) { $articles = Model::factory('Article')->order_by_desc('timestamp')->find_many(); $isLoginned = User::isLoginned(); $user_name = ''; if ($isLoginned) { $user_name = $_COOKIE['login']; } return $app->render('blog_home.html', array('articles' => $articles, 'isLoginned' => $isLoginned, 'B_author' => 'People', 'User_name' => $user_name)); }); // Blog View.
// End FILTERS /* == * * * ROUTES * * ==============================================*/ $app->map('/', function () use($app) { if ($app->request()->isPost() && sizeof($app->request()->post()) == 2) { // if valid login, set auth cookie and redirect $testp = sha1('uAX8+Tdv23/3YQ=='); $post = (object) $app->request()->post(); if (isset($post->username) && isset($post->password) && sha1($post->password) == $testp && $post->username == 'bppenne') { //$app->setEncryptedCookie('bppasscook', $post->password, 0); $app->setCookie('user_cook', $post->username, 0); $app->setCookie('pass_cook', $post->password, 0); $app->redirect('./review'); } else { $app->redirect('.'); } } $app->render('login.html'); })->via('GET', 'POST')->name('login'); $authUser = function ($role = 'member') use($app) { return function () use($role) { $app = Slim::getInstance(); // Check for password in the cookie if ($app->getCookie('pass_cook') != 'uAX8+Tdv23/3YQ==' || $app->getCookie('user_cook') != 'bppenne') { //if ( $app->getEncryptedCookie('bppasscook', false) != 'uAX8+Tdv23/3YQ==') { $app->redirect('..'); //$app->redirect('review'); }
/** * Test Slim::redirect * * Pre-conditions: * Case A: Status code is less than 300 * Case B: Status code is greater than 307 * Case C: Status code is 300 * Case D: Status code is 302 (between 300 and 307) * Case E: Status code is 307 * * Post-conditions: * Case A: Response code is 500 (due to invalid redirect status) * Case B: Response code is 500 (due to invalid redirect status) * Case C: Response code is 300 * Case D: Response code is 302 * Case E: Response code is 307 */ public function testSlimRedirect() { //Case A $app1 = new Slim(); $app1->get('/', function () use($app1) { $app1->redirect('/foo', 200); }); ob_start(); $app1->run(); $app1Out = ob_get_clean(); $this->assertEquals(500, $app1->response()->status()); //Case B $app2 = new Slim(); $app2->get('/', function () use($app2) { $app2->redirect('/foo', 308); }); ob_start(); $app2->run(); $app2Out = ob_get_clean(); $this->assertEquals(500, $app2->response()->status()); //Case C $app3 = new Slim(); $app3->get('/', function () use($app3) { $app3->redirect('/foo', 300); }); $app3->run(); $this->assertEquals(300, $app3->response()->status()); //Case D $app4 = new Slim(); $app4->get('/', function () use($app4) { $app4->redirect('/foo', 302); }); $app4->run(); $this->assertEquals(302, $app4->response()->status()); //Case E $app5 = new Slim(); $app5->get('/', function () use($app5) { $app5->redirect('/foo', 307); }); $app5->run(); $this->assertEquals(307, $app5->response()->status()); }
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(); $newPath = preg_replace("#/{2,}#", '/', $path); if ($path != $newPath) { $app->redirect($request->getUrl() . $newPath, 301); } // process referrer tag if (isset($_GET['ref']) && isset($referrers[$_GET['ref']])) { $_SESSION['ref'] = $_GET['ref']; $_SESSION['tag'] = $referrers[$_GET['ref']]; } else { $_SESSION['ref'] = null; } }); $routes = (include 'routes.php'); // Use Slim with Class#method style routes foreach ($routes as $name => $details) { $fn = function () use($details) { list($class, $method) = explode('.', $details[1]); $class = "{$class}Controller";
require "libs/Simplepie/autoloader.php"; //Autoload para as Classes do SimplePie, para leitura de RSS require "libs/Slim/Slim.php"; //Micro-framework Slim, para gerenciamento de rotas e alguns Helpers include "app/funcoes.php"; //Funções próprias, como CSS, Javascript e Meta include "app/config.php"; //Configurações gerais do sistema, através de Constantes. date_default_timezone_set('America/Sao_Paulo'); $autoloader = new Autoloader(); $app = new Slim(); $app->contentType('text/html; charset=utf-8'); $app->add(new Slim_Middleware_SessionCookie(array('secret' => '98897qwer65465qwe9r79qw9e354as68dh56k6lks6df8g', 'expires' => '60 minutes'))); $authenticate = function ($app) { return function () use($app) { if (!isset($_SESSION['dehbora']['user'])) { $_SESSION['dehbora']['urlRedirect'] = $app->request()->getPathInfo(); $app->flash('error', 'Você precisa se logar.'); $app->redirect(URL_BASE . '/inicial'); } }; }; $app->hook('slim.before.dispatch', function () use($app) { $user = null; if (isset($_SESSION['dehbora']['user'])) { $user = $_SESSION['dehbora']['user']; } $app->view()->setData('user', $user); }); require_once "app/routes.php"; $app->run();
/** * Test Slim::redirect * * Pre-conditions: * Case A: Status code is less than 300 * Case B: Status code is greater than 307 * Case C: Status code is 300 * Case D: Status code is 302 (between 300 and 307) * Case E: Status code is 307 * * Post-conditions: * Case A: An InvalidArgumentException is thrown * Case B: An InvalidArgumentException is thrown * Case C: Response code is 300 * Case D: Response code is 302 * Case E: Response code is 307 */ public function testSlimRedirect() { //Case A Slim::init(); Slim::get('/', function () { Slim::redirect('/foo', 200); }); try { Slim::run(); $this->fail('InvalidArgumentException not caught'); } catch( InvalidArgumentException $e ) {} //Case B Slim::init(); Slim::get('/', function () { Slim::redirect('/foo', 308); }); try { Slim::run(); $this->fail('InvalidArgumentException not caught'); } catch( InvalidArgumentException $e ) {} //Case C Slim::init(); Slim::get('/', function () { Slim::redirect('/foo', 300); }); try { Slim::run(); $this->fail("SlimStopException not caught"); } catch ( Slim_Exception_Stop $e ) {} $this->assertEquals(Slim::response()->status(), 300); //Case D Slim::init(); Slim::get('/', function () { Slim::redirect('/foo', 302); }); try { Slim::run(); $this->fail("SlimStopException not caught"); } catch ( Slim_Exception_Stop $e ) {} $this->assertEquals(Slim::response()->status(), 302); //Case E Slim::init(); Slim::get('/', function () { Slim::redirect('/foo', 307); }); try { Slim::run(); $this->fail("SlimStopException not caught"); } catch ( Slim_Exception_Stop $e ) {} $this->assertEquals(Slim::response()->status(), 307); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; // Prepare app $app = new Slim(array('negotiation.enabled' => true)); // Setup routes $app->get('/', function () use($app) { $format = $app->respondTo('html', 'rdf', 'ttl', 'json', 'nt'); switch ($format) { case 'html': return $app->redirect('http://www.aelius.com/njh/', 303); default: $rootUrl = $app->request()->getUrl() . $app->request()->getScriptName(); return $app->redirect("{$rootUrl}/foaf.{$format}", 303); } }); $app->get('/foaf:format', function () use($app) { $format = $app->respondTo('rdf', 'ttl', 'json', 'nt'); $uri = $app->request()->getUrl() . $app->request()->getPath(); $foaf = new EasyRdf_Graph($uri); $foaf->parseFile(__DIR__ . '/../data/foaf.ttl', 'turtle', $uri); $app->response()->body($foaf->serialise($format)); }); // Run app $app->run();
/** * Performs redirect * * @param string $path */ protected function redirect($path) { $this->app->redirect($path); }
return $app->render('admin_home.html', array('articles' => $articles)); }); // Admin Add. $app->get('/admin/add', $authCheck, function () use($app) { return $app->render('admin_input.html', array('action_name' => 'Add', 'action_url' => '/admin/add')); }); // Admin Add - POST. $app->post('/admin/add', $authCheck, function () use($app) { $article = Model::factory('Article')->create(); $article->title = $app->request()->post('title'); $article->author = $app->request()->post('author'); $article->summary = $app->request()->post('summary'); $article->content = $app->request()->post('content'); $article->timestamp = date('Y-m-d H:i:s'); $article->save(); $app->redirect('/admin'); }); // Admin Edit. $app->get('/admin/edit/(:id)', $authCheck, function ($id) use($app) { $article = Model::factory('Article')->find_one($id); if (!$article instanceof Article) { $app->notFound(); } return $app->render('admin_input.html', array('action_name' => 'Edit', 'action_url' => '/admin/edit/' . $id, 'article' => $article)); }); // Admin Edit - POST. $app->post('/admin/edit/(:id)', $authCheck, function ($id) use($app) { $article = Model::factory('Article')->find_one($id); if (!$article instanceof Article) { $app->notFound(); }