Skip to content

Commit b44e678

Browse files
authored
Merge pull request #10 from samsonasik/use-parent-handle
refactor: use parent::handle() call on PageRouter, use page based route only on fallback
2 parents eb8f6aa + 8217889 commit b44e678

File tree

2 files changed

+10
-54
lines changed

2 files changed

+10
-54
lines changed

src/Commands/PageCreateCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function run(array $params)
5151
[
5252
'{{pageName}}' => $pageName,
5353
'{{pageNamespace}}' => str_replace('/', '\\', $pageName),
54-
]
54+
],
5555
);
5656

5757
// Create the index.php file
@@ -61,7 +61,7 @@ public function run(array $params)
6161
[
6262
'{{pageName}}' => $pageName,
6363
'{{pageSlug}}' => str_replace('/', '_', $pageName),
64-
]
64+
],
6565
);
6666
}
6767

src/PageRouter.php

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -42,46 +42,18 @@ class PageRouter extends Router
4242
*/
4343
public function handle(?string $uri = null)
4444
{
45-
// If we cannot find a URI to match against, then set it to root (`/`).
46-
if ($uri === null || $uri === '') {
47-
$uri = '/';
48-
}
49-
50-
// Decode URL-encoded string
51-
$uri = urldecode($uri);
52-
53-
$this->checkDisallowedChars($uri);
54-
55-
// Restart filterInfo
56-
$this->filtersInfo = [];
57-
58-
// Checks defined routes
59-
if ($this->checkRoutes($uri)) {
60-
if ($this->collection->isFiltered($this->matchedRoute[0])) {
61-
$this->filtersInfo = $this->collection->getFiltersForRoute($this->matchedRoute[0]);
45+
try {
46+
$handle = parent::handle($uri);
47+
} catch (PageNotFoundException $e) {
48+
// HACK: Check for page based routes
49+
if ($this->pageBasedRoute($uri)) {
50+
return $this->controllerName();
6251
}
6352

64-
return $this->controller;
65-
}
66-
67-
// HACK: Check for page based routes
68-
if ($this->pageBasedRoute($uri)) {
69-
return $this->controllerName();
70-
}
71-
72-
// Still here? Then we can try to match the URI against
73-
// Controllers/directories, but the application may not
74-
// want this, like in the case of API's.
75-
if (! $this->collection->shouldAutoRoute()) {
76-
throw new PageNotFoundException(
77-
"Can't find a route for '{$this->collection->getHTTPVerb()}: {$uri}'."
78-
);
53+
throw $e;
7954
}
8055

81-
// Checks auto routes
82-
$this->autoRoute($uri);
83-
84-
return $this->controllerName();
56+
return $handle;
8557
}
8658

8759
/**
@@ -134,20 +106,4 @@ public function pageBasedRoute(string $uri)
134106

135107
return $pageFound;
136108
}
137-
138-
/**
139-
* Checks disallowed characters
140-
*/
141-
private function checkDisallowedChars(string $uri): void
142-
{
143-
foreach (explode('/', $uri) as $segment) {
144-
if ($segment !== '' && $this->permittedURIChars !== ''
145-
&& preg_match('/\A[' . $this->permittedURIChars . ']+\z/iu', $segment) !== 1
146-
) {
147-
throw new BadRequestException(
148-
'The URI you submitted has disallowed characters: "' . $segment . '"'
149-
);
150-
}
151-
}
152-
}
153109
}

0 commit comments

Comments
 (0)