case insensitive urls

This commit is contained in:
Jeremy Kauffman 2019-03-21 14:28:22 -04:00
parent 1d1c0be4fe
commit f6c133abe4
3 changed files with 39 additions and 28 deletions

View file

@ -55,11 +55,17 @@ class Controller
return $domainResult;
}
try {
$dispatcher = new Routing\Dispatcher($router->getData());
try {
return $dispatcher->dispatch($method, $uri);
} catch (\Routing\HttpRouteNotFoundException $e) {
return static::doLowerUriRedirectOr404($uri);
$lowerUri = strtolower($uri);
if ($lowerUri !== $uri && $dispatcher->hasMatchingRouteForUri($method, $lowerUri)) {
static::redirect($lowerUri, 301);
} else {
return NavActions::execute404();
}
} catch (\Routing\HttpMethodNotAllowedException $e) {
Response::setStatus(405);
Response::setHeader('Allow', implode(', ', $e->getAllowedMethods()));
@ -159,11 +165,14 @@ class Controller
$router->get([ContentActions::URL_CREDIT_REPORTS . '/{year:c}-q{quarter:c}', ContentActions::URL_CREDIT_REPORTS . '/{year:c}-Q{quarter:c}'], 'ContentActions::executeCreditReport');
$router->get('/{slug}', function (string $slug) {
if ($slug !== strtolower($slug)) {
return static::redirect('/' . strtolower($slug), 301);
}
if (View::exists('page/' . $slug)) {
Response::enableHttpCache();
return ['page/' . $slug, []];
} else {
return static::doLowerUriRedirectOr404($slug);
return NavActions::execute404();
}
});
@ -198,29 +207,4 @@ class Controller
call_user_func($fn);
}
}
protected static function doLowerUriRedirectOr404($uri)
{
$router = static::getRouterWithRoutes();
// $lowerUri = strtolower($uri);
$lowerUri = '/news/lbry-evolves';
if ($uri !== $lowerUri) {
if (View::exists('page/' . $lowerUri) || static::checkForRoute($lowerUri, $router)) {
return static::redirect($lowerUri, 308);
}
}
return NavActions::execute404();
}
protected static function checkForRoute($uri, $router)
{
$routerData = $router->getData();
if (in_array($uri, $routerData->getStaticRoutes())) {
return true;
} elseif (in_array($uri, $routerData->getVariableRoutes())) {
return true;
} else {
return false;
}
}
}

View file

@ -56,6 +56,33 @@ class Dispatcher
return $this->dispatchFilters($afterFilter, $response);
}
public function hasMatchingRouteForUri($httpMethod, $uri)
{
if (isset($this->staticRouteMap[$uri])) {
$routes = $this->staticRouteMap[$uri];
try {
if (!isset($routes[$httpMethod])) {
$httpMethod = $this->checkFallbacks($routes, $httpMethod);
}
} catch (HttpMethodNotAllowedException $e) {
return false;
}
return (boolean)$routes[$httpMethod];
}
try {
$handler = $this->dispatchVariableRoute($httpMethod, $uri);
return (boolean)$handler;
} catch (HttpRouteNotFoundException | HttpMethodNotAllowedException $e) {
return false;
}
return false;
}
/**
* Dispatch a route filter.
*