Skip to content

Commit a8c72a0

Browse files
committed
RouteList: added addRoute() & withModule()
1 parent fc1a9f7 commit a8c72a0

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

src/Application/Routers/RouteList.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,31 @@ public function constructUrl(array $params, Nette\Http\UrlScript $refUrl): ?stri
6262
}
6363

6464

65+
/**
66+
* @param string $mask e.g. '<presenter>/<action>/<id \d{1,3}>'
67+
* @param array|string|\Closure $metadata default values or metadata or callback for NetteModule\MicroPresenter
68+
* @return static
69+
*/
70+
public function addRoute(string $mask, $metadata = [], int $flags = 0)
71+
{
72+
$this->add(new Route($mask, $metadata), $flags);
73+
return $this;
74+
}
75+
76+
77+
/**
78+
* @return static
79+
*/
80+
public function withModule(string $module)
81+
{
82+
$router = new static;
83+
$router->module = $module . ':';
84+
$router->parent = $this;
85+
$this->add($router);
86+
return $router;
87+
}
88+
89+
6590
public function getModule(): ?string
6691
{
6792
return $this->module;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\Application\Routers\RouteList;
6+
7+
8+
require __DIR__ . '/../bootstrap.php';
9+
10+
require __DIR__ . '/Route.php';
11+
12+
13+
$list = new RouteList;
14+
$list->addRoute('foo', ['presenter' => 'foo'], RouteList::ONE_WAY);
15+
$list->addRoute('bar', ['presenter' => 'bar'], RouteList::ONE_WAY);
16+
$list->addRoute('hello', ['presenter' => 'hello']);
17+
18+
19+
testRouteIn($list, '/foo', ['presenter' => 'foo', 'test' => 'testvalue']);
20+
21+
testRouteIn($list, '/bar', ['presenter' => 'bar', 'test' => 'testvalue']);
22+
23+
testRouteIn($list, '/hello',
24+
['presenter' => 'hello', 'test' => 'testvalue'],
25+
'/hello?test=testvalue'
26+
);
27+
28+
testRouteIn($list, '/none');
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\Application\Routers\RouteList;
6+
7+
8+
require __DIR__ . '/../bootstrap.php';
9+
10+
require __DIR__ . '/Route.php';
11+
12+
13+
$list = new RouteList;
14+
$list
15+
->withModule('A')
16+
->addRoute('foo', ['presenter' => 'foo'])
17+
->withModule('B')
18+
->addRoute('bar', ['presenter' => 'bar'])
19+
->end()
20+
->end()
21+
->withModule('C')
22+
->addRoute('hello', ['presenter' => 'hello']);
23+
24+
25+
testRouteIn($list, '/foo',
26+
['presenter' => 'A:foo', 'test' => 'testvalue'],
27+
'/foo?test=testvalue'
28+
);
29+
30+
testRouteIn($list, '/bar',
31+
['presenter' => 'A:B:bar', 'test' => 'testvalue'],
32+
'/bar?test=testvalue'
33+
);
34+
35+
testRouteIn($list, '/hello',
36+
['presenter' => 'C:hello', 'test' => 'testvalue'],
37+
'/hello?test=testvalue'
38+
);
39+
40+
testRouteIn($list, '/none');

0 commit comments

Comments
 (0)