Example #1
0
 /**
  * Finds a route that matches the request
  * @param  \snb\http\Request $request
  * @return Route
  */
 public function findMatchingRoute(Request $request)
 {
     // go though all the routes in order and return the first matching one
     $path = urldecode($request->getPath());
     foreach ($this->routes as $route) {
         if ($route->isMatch($path, $request)) {
             return $route;
         }
     }
     return null;
 }
Example #2
0
 /**
  * Testing the same URL with different data types for the placeholders
  */
 public function testRouteWrongDataType()
 {
     // create a request
     $request = Request::create('/blog/test/42');
     // Create a route
     $r = new Route('blogtest', '/blog/test/{page}', array('controller' => 'example:DemoController:hello'), array('page' => 'int'), array());
     // Test that the route matches the request
     $this->assertTrue($r->isMatch('/blog/test/42', $request));
     $r = new Route('another', '/blog/test/{page}', array('controller' => 'example:DemoController:hello'), array('page' => 'alpha'), array());
     $this->assertFalse($r->isMatch('/blog/test/42', $request));
 }
Example #3
0
 /**
  * Same again, only for POST arguments
  */
 public function testPostArguments()
 {
     $request = Request::create('http://www.example.com/blog/42/testing-routes');
     $post = $request->post;
     $this->assertEquals($post->count(), 0);
     $request = Request::create('blog/42', 'POST', array('x' => 42, 'long_name' => 'Lots of data here'));
     $post = $request->post;
     $this->assertEquals($post->count(), 2);
     $this->assertTrue($post->has('x'));
     $this->assertTrue($post->has('long_name'));
     $this->assertFalse($post->has('y'));
     $request = Request::create('blog/42', 'POST', array('x' => 42, 'y' => '42 text message! £3.40 #trouble'));
     $post = $request->post;
     $x = $post->get('x');
     $this->assertEquals($x, 42);
     // get an int
     $x = $post->getInt('x');
     $this->assertSame($x, 42);
     $this->assertTrue(is_int($x));
     // get as text
     $text = $post->getText('x');
     $this->assertSame($text, '42');
     $this->assertTrue(is_string($text));
     // get only alpha chars
     $text = $post->getAlpha('y');
     $this->assertSame($text, 'textmessagetrouble');
     $this->assertTrue(is_string($text));
     // get only alpha num chars
     $text = $post->getAlphaNum('y');
     $this->assertSame($text, '42textmessage340trouble');
     // get only digit chars
     $text = $post->getDigits('y');
     $this->assertSame($text, '42340');
     // get something that isn't there
     $text = $post->getText('not_there');
     $this->assertSame($text, '');
     $text = $post->getText('not_there', 'default value');
     $this->assertSame($text, 'default value');
     $text = $post->getText('x', 'default value');
     $this->assertSame($text, '42');
 }
Example #4
0
 /**
  * Determines if the form has been successfully submitted
  * if this returns true, you can process the form and redirect.
  * if it returns false, then you need to draw the form.
  * @param  \snb\http\Request $request
  * @return bool
  */
 public function onPostValid(Request $request)
 {
     // If this is a post request
     if ($request->getMethod() == 'POST') {
         // bind the post data to the form
         $this->bindRequest($request);
         // check that the form data is valid
         if ($this->isValid()) {
             return true;
         }
     }
     // not ready to be processed yet
     return false;
 }
Example #5
0
 /**
  * isMatch
  * Trys to match the url of the request to this route
  * @param  string            $path    - the is the urldecoded path in the url
  * @param  \snb\http\Request $request
  * @return bool
  */
 public function isMatch($path, Request $request)
 {
     // See if it is a match for this route
     $regex = $this->getRegex();
     if (!preg_match($regex, $path, $regs)) {
         return false;
     }
     // OK, this URL is a good one
     // Check that the request method matches the route
     $method = $this->getMethod();
     $m = explode('|', mb_strtoupper($method));
     if (!in_array($request->getMethod(), $m)) {
         return false;
     }
     // Something similar for the protocol
     $protocol = $this->getProtocol();
     $p = explode('|', mb_strtolower($protocol));
     if (!in_array($request->getProtocol(), $p)) {
         return false;
     }
     // It is, so prepare the variables and create the controller
     $p = array();
     foreach ($this->vars as $key => $name) {
         // copy over the default value if there is one
         if (array_key_exists($name, $this->defaults)) {
             $p[$name] = $this->defaults[$name];
         }
         // replace it with the value from url, if it was included
         if (array_key_exists($key + 1, $regs)) {
             $p[$name] = $regs[$key + 1];
         }
     }
     // remember these, as someone it likely to want them very soon
     $this->matchedArgs = $p;
     $this->matchedUri = $request->getUri();
     // This route was a match
     return true;
 }
Example #6
0
<?php

/**
 * This file is part of the Small Neat Box Framework
 * Copyright (c) 2011-2012 Small Neat Box Ltd.
 * For the full copyright and license information, please view the LICENSE.txt
 * file that was distributed with this source code.
 */
// Prepare the autoloader
require_once __DIR__ . '/../app/autoload.php';
require_once __DIR__ . '/../app/AppKernel.php';
use snb\http\Request;
// Work out the environment. this is set in Apache to dev or live via the rewrite rule.
// eg. RewriteRule ^(.*)$ app.php [QSA,E=BAND_ENV:dev,L]
// or directly: SetEnv BAND_ENV dev
$env = 'dev';
if (isset($_SERVER['BAND_ENV'])) {
    $env = $_SERVER['BAND_ENV'];
} else {
    if (isset($_SERVER['REDIRECT_BAND_ENV'])) {
        $env = $_SERVER['REDIRECT_BAND_ENV'];
    }
}
// Create the app kernel and handle the request
$app = new AppKernel($env);
$app->handle(Request::createFromGlobals())->send();
Example #7
0
 public function setRedirectToRoute(Route $route, $args = array(), Request $context = null)
 {
     // set up the response to redirect to a named route
     if (!$route) {
         return;
     }
     // generate the url for the route
     $url = $route->generate($args);
     // add the domain if we can
     if ($context) {
         $url = $context->getHttpHost() . $url;
     }
     // Finally, set the redirect headers
     $this->setRedirectToURL($url);
 }