Routes
Routes are virtual paths like /dashboard or /upload. They don't exist as real folders on the server. Chevereto maps each request to a PHP route file.
You can add your own routes, or override the ones that ship with Chevereto, without editing the core files.
| Path | Purpose |
|---|---|
app/legacy/routes/ | Built-in web routes, one file per route |
app/legacy/routes/overrides/ | Your custom routes and overrides |
content/legacy/themes/Peafowl/ | Theme views rendered by routes |
app/routes/api/5/ | API V5 routes |
How routing works
Chevereto uses the first segment of the request path as the route name and loads the file with the same name:
| Request | Route file |
|---|---|
/ | app/legacy/routes/index.php |
/upload | app/legacy/routes/upload.php |
/dashboard/images | app/legacy/routes/dashboard.php |
The rest of the path (images in /dashboard/images) is passed to the route, which decides what to do with it.
When the first segment doesn't match a route file, Chevereto tries the following, in order:
- Virtual routes. If the segment matches the path configured for images, albums, users, videos, or audio (for example
/imgif you renamed/image), the request goes to that route. - Root route. Otherwise the request goes to the route set as
root_route(userby default). This is how/usernameopens a user profile.
Route overrides
app/legacy/routes/overrides/
Any file placed in overrides/ takes precedence over the built-in route with the same name. If there is no built-in route with that name, the file becomes a new route.
For example, to customize the /upload route:
- Copy
app/legacy/routes/upload.phptoapp/legacy/routes/overrides/upload.php(create theoverridesfolder if it doesn't exist). - Edit the copy.
Chevereto now uses your file for /upload. To restore the default, delete the override.
Keep track of changes
When you upgrade Chevereto, built-in routes may change. Compare your overrides against the new version of the original files and merge any changes you need.
Internal includes
Some routes load other routes directly from app/legacy/routes/, skipping overrides. For example /video reuses image.php, and the homepage can reuse the route set in Homepage style. To change those, override the calling route too (video.php, index.php).
Add a new route
A route file returns a function that gets the request Handler. Create app/legacy/routes/overrides/hello.php:
<?php
use Chevereto\Legacy\G\Handler;
return function (Handler $handler) {
// Only accept /hello, return 404 on /hello/anything
if ($handler->isRequestLevel(2)) {
$handler->issueError(404);
return;
}
$handler::setVar('pre_doctitle', 'Hello');
$handler::setVar('greeting', 'Hello, world!');
};Now /hello runs your route. By default it renders the theme view with the same name as the route, so create content/legacy/themes/Peafowl/overrides/views/hello.php:
<?php
use Chevereto\Legacy\G\Handler;
use function Chevereto\Legacy\G\require_theme_footer;
use function Chevereto\Legacy\G\require_theme_header;
require_theme_header(); ?>
<div class="content-width">
<h1><?php echo Handler::var('greeting'); ?></h1>
</div>
<?php require_theme_footer(); ?>See Theme for how view overrides work.
Route names
Route names can only contain letters, numbers, -, _ and .. A new route takes over that path, so /hello will no longer open the profile of a user named hello. It also takes precedence over a virtual route that uses the same path.
Handler reference
Inside a route, use $handler to read the request and decide the response.
Request
| Method | Returns |
|---|---|
$handler->requestArray() | All path segments. For /hello/a/b: ['hello', 'a', 'b'] |
$handler->request() | Segments after the route name. For /hello/a/b: ['a', 'b'] |
$handler->isRequestLevel(n) | true if segment n exists (1-based, 1 is the route name) |
$handler::checkCSRFToken() | true if the request carries a valid CSRF token |
To read query strings and form data use Chevereto\Vars\get() and Chevereto\Vars\post().
Response
| Method | Effect |
|---|---|
$handler::setVar($name, $value) | Pass a value to the view, read it with Handler::var($name) |
$handler::setVars([...]) | Set several view variables at once |
$handler::setCond($name, $bool) | Set a condition, read it with Handler::cond($name) |
$handler->setTemplate($name) | Render a different view than the route name |
$handler->setContent($html) | Render $html inside the theme header and footer, no view |
$handler->issueError($status) | Respond with an error status and its error view (404, 403) |
$handler->mapRoute($route) | Hand the request over to another route |
Setting the pre_doctitle variable prepends it to the page <title>.
Views also get these variables: post, get, request, their HTML-escaped versions safe_post, safe_get, safe_request, and csrf_token.
Non-HTML responses
To respond with something other than a theme page, send your headers, output the body and call exit(). This skips the view:
<?php
use Chevereto\Legacy\G\Handler;
return function (Handler $handler) {
header('Content-Type: application/json');
echo json_encode(['status' => 'ok']);
exit();
};Private mode
When the website is in private mode, guests trying to access your custom routes are redirected to the login page.
API V5 routes
Requests starting with /api/5/ are handled by a separate router, defined at app/routes/api/5/. These routes are not part of the override system.
To add your own API endpoints, add a web route that responds with JSON.
Docker
In Docker the application lives at /var/www/html. Mount your overrides folder so it survives container rebuilds:
volumes:
- ./routes-overrides:/var/www/html/app/legacy/routes/overrides