Skip to content

Load routes from `site/routes` and annotate with PHP8 attributes

License

Notifications You must be signed in to change notification settings

presprog/kirby-route-attributes

Repository files navigation

Kirby routing with attributes

Load routes from site/routes and annotate metadata with PHP 8 attributes. No more business logic in your config files 🎉


Usage

After installing, create a new folder site/routes. Then create a new file for each route you want to add. The filename does not matter, but every file must return a function, that is annotated with the provided PHP 8 attributes:

// site/routes/my-awesome-route.php
<?php

use Kirby\Http\Response;
use PresProg\RouteAttributes\Attributes\Get;

return #[Get('/foo/(:all)')] function ($all) {
    return new Response("<h1>$all</h1>", null, 200);
};

The following attributes are included:

All these attributes share the same base Route attribute, which you also may use directly too:

// site/routes/my-awesome-route.php
<?php

use Kirby\Http\Response;
use PresProg\RouteAttributes\Attributes\Route;

return #[Route('/foo/(:all)', 'GET')] function ($all) {
    return new Response("<h1>$all</h1>", null, 200);
};

When developing in debug mode, the files will always be read from disk. Whendebug is false, the loaded routes will be read once and then cached. Make sure to clear the plugin cache after deployment, when you have made changes to the routes.


Installation

Composer

composer require presprog/kirby-route-attributes

Download

Download and copy this repository to /site/plugins/route-attributes.

Git submodule

git submodule add https://github.com/presprog/kirby-route-attributes.git site/plugins/route-attributes

License

MIT

Credits

Designed and built with ☕ and ❤ by Present Progressive

About

Load routes from `site/routes` and annotate with PHP8 attributes

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages