Skip to content

Commit

Permalink
Working on routing.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 13, 2013
1 parent f48e599 commit ee8639d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
11 changes: 6 additions & 5 deletions src/Illuminate/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,15 @@ public function uriExpression($delimit = true)
public static function compileString($value, $delimit = true, array $wheres = array())
{
$value = static::prepareString($value);

$value = static::compileOptional(static::compileParameters($value, $wheres), $wheres);

return $delimit ? static::delimit($value) : $value;
}

/**
* Prepare a string for use as regular expression.
*
*
* @param string $value
* @return string
*/
Expand All @@ -468,7 +469,7 @@ protected static function compileParameters($value, array $wheres = array())
{
$value = static::compileWhereParameters($value, $wheres);

return preg_replace('/\{([A-Za-z\-\_]+)\}/', static::$wildcard, $value);
return preg_replace('/\{([A-Za-z\-\_]+?)\}/', static::$wildcard, $value);
}

/**
Expand Down Expand Up @@ -511,9 +512,9 @@ protected static function compileOptional($value, $wheres = array())
*/
protected static function compileStandardOptional($value, $custom = 0)
{
$value = preg_replace('/\/\{([A-Za-z\-\_]+)\?\}/', static::$optional, $value, -1, $count);
$value = preg_replace('/\/\{([A-Za-z\-\_]+?)\?\}/', static::$optional, $value, -1, $count);

$value = preg_replace('/^(\{([A-Za-z\-\_]+)\?\})/', static::$leadingOptional, $value, -1, $leading);
$value = preg_replace('/^(\{([A-Za-z\-\_]+?)\?\})/', static::$leadingOptional, $value, -1, $leading);

$total = $leading + $count + $custom;

Expand Down
29 changes: 24 additions & 5 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class RoutingRouteTest extends PHPUnit_Framework_TestCase {


public function testBasicDispatchingOfRoutes()
{
$router = $this->getRouter();
Expand Down Expand Up @@ -67,6 +68,24 @@ public function testBasicDispatchingOfRoutes()
}


public function testNonGreedyMatches()
{
$route = new Route('GET', 'images/{id}.{ext}', function() {});

$request1 = Request::create('images/1.png', 'GET');
$this->assertTrue($route->matches($request1));
$route->bind($request1);
$this->assertEquals('1', $route->parameter('id'));
$this->assertEquals('png', $route->parameter('ext'));

$request2 = Request::create('images/12.png', 'GET');
$this->assertTrue($route->matches($request2));
$route->bind($request2);
$this->assertEquals('12', $route->parameter('id'));
$this->assertEquals('png', $route->parameter('ext'));
}


/**
* @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
Expand Down Expand Up @@ -342,24 +361,24 @@ public function testWherePatternsProperlyFilter()
$route->where('bar', '[0-9]+');
$this->assertFalse($route->matches($request));
}


public function testDotDoesNotMatchEverything()
{
$route = new Route('GET', 'images/{id}.{ext}', function() {});

$request1 = Request::create('images/1.png', 'GET');
$this->assertTrue($route->matches($request1));
$route->bind($request1);
$this->assertEquals('1', $route->parameter('id'));
$this->assertEquals('png', $route->parameter('ext'));

$request2 = Request::create('images/12.png', 'GET');
$this->assertTrue($route->matches($request2));
$route->bind($request2);
$this->assertEquals('12', $route->parameter('id'));
$this->assertEquals('png', $route->parameter('ext'));

}


Expand Down

0 comments on commit ee8639d

Please sign in to comment.