Skip to content

Instantly share code, notes, and snippets.

@ssddanbrown
Created June 28, 2021 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssddanbrown/35c3c1f2974f3892778a9ab372b70a85 to your computer and use it in GitHub Desktop.
Save ssddanbrown/35c3c1f2974f3892778a9ab372b70a85 to your computer and use it in GitHub Desktop.
BookStack root URI page search via theme functions file
<?php
use BookStack\Entities\Models\Page;
use BookStack\Theming\ThemeEvents;
use BookStack\Facades\Theme;
use Illuminate\Http\Request;
// This will be the content of the functions.php file you'll have
// after following the getting started instructions here:
// https://github.com/BookStackApp/BookStack/blob/release/dev/docs/logical-theme-system.md
Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, function (Request $request, $response) {
// Ignore if not 404 or a GET response.
if ($response->getStatusCode() !== 404 && $request->getMethod() !== 'GET') {
return $response;
}
// Get a path and ignore if more than one path segment.
$path = $request->decodedPath();
if (str_contains('/', $path)) {
return $response;
}
// Find a matching page and redirect to that if existing
// otherwise return the default response.
$page = Page::visible()->where('name', '=', $path)->first();
return $page ? redirect($page->getUrl()) : $response;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment