Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6ea8d8b

Browse files
committed
Work on leading optional parameters.
1 parent 694d624 commit 6ea8d8b

4 files changed

Lines changed: 41 additions & 2 deletions

File tree

src/Illuminate/Routing/Matching/UriValidator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class UriValidator implements ValidatorInterface {
1414
*/
1515
public function matches(Route $route, Request $request)
1616
{
17+
//dd($route->uriExpression());
18+
//return dd(preg_match('#^(\/)$|^(?:([a-zA-Z0-9\.\-_%=]+))?$#u', 'foo/bar'));
1719
return preg_match($route->uriExpression(), $request->path());
1820
}
1921

src/Illuminate/Routing/Route.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ class Route {
7272
*/
7373
protected static $optional = '(?:/([a-zA-Z0-9\.\-_%=]+)';
7474

75+
/**
76+
* The regular expression for a leading optional wildcard.
77+
*
78+
* @var string
79+
*/
80+
protected static $leadingOptional = '\/$|^(?:([a-zA-Z0-9\.\-_%=]+)';
81+
7582
/**
7683
* The validators used by the routes.
7784
*
@@ -487,7 +494,11 @@ protected static function compileStandardOptional($value, $custom = 0)
487494
{
488495
$value = preg_replace('/\/\{(.*?)\?\}/', static::$optional, $value, -1, $count);
489496

490-
return $count + $custom > 0 ? $value .= str_repeat(')?', $count + $custom) : $value;
497+
$value = preg_replace('/^(\{(.*?)\?\})/', static::$leadingOptional, $value, -1, $leading);
498+
499+
$total = $leading + $count + $custom;
500+
501+
return $total > 0 ? $value .= str_repeat(')?', $total) : $value;
491502
}
492503

493504
/**

src/Illuminate/Routing/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ protected function createRoute($methods, $uri, $action)
654654

655655
$route = with(new Route(
656656
$methods, $uri = $this->prefix($uri), $action)
657-
);
657+
);
658658

659659
$route->where($this->patterns);
660660

tests/Routing/RoutingRouteTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,32 @@ public function testBasicDispatchingOfRoutes()
2929
$router = $this->getRouter();
3030
$router->get('foo/{name}/boom/{age?}/{location?}', function($name, $age = 25, $location = 'AR') { return $name.$age.$location; });
3131
$this->assertEquals('taylor30AR', $router->dispatch(Request::create('foo/taylor/boom/30', 'GET'))->getContent());
32+
33+
$router = $this->getRouter();
34+
$router->get('{bar}/{baz?}', function($name, $age = 25) { return $name.$age; });
35+
$this->assertEquals('taylor25', $router->dispatch(Request::create('taylor', 'GET'))->getContent());
36+
37+
$router = $this->getRouter();
38+
$router->get('{baz?}', function($age = 25) { return $age; });
39+
$this->assertEquals('25', $router->dispatch(Request::create('/', 'GET'))->getContent());
40+
$this->assertEquals('30', $router->dispatch(Request::create('30', 'GET'))->getContent());
41+
42+
$router = $this->getRouter();
43+
$router->get('{foo?}/{baz?}', function($name = 'taylor', $age = 25) { return $name.$age; });
44+
$this->assertEquals('taylor25', $router->dispatch(Request::create('/', 'GET'))->getContent());
45+
$this->assertEquals('fred25', $router->dispatch(Request::create('fred', 'GET'))->getContent());
46+
$this->assertEquals('fred30', $router->dispatch(Request::create('fred/30', 'GET'))->getContent());
47+
}
48+
49+
50+
/**
51+
* @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
52+
*/
53+
public function testRoutesDontMatchNonMatchingPathsWithLeadingOptionals()
54+
{
55+
$router = $this->getRouter();
56+
$router->get('{baz?}', function($age = 25) { return $age; });
57+
$this->assertEquals('25', $router->dispatch(Request::create('foo/bar', 'GET'))->getContent());;
3258
}
3359

3460

0 commit comments

Comments
 (0)