Osumi Framework
es en eu v9.8.5 GitHub

Routing

Routing in Osumi Framework is managed by the ORoute class. It maps incoming HTTP requests (URLs) to specific Components that act as actions.

Routes are typically defined in PHP files located within the src/Routes/ directory. You can create multiple files in this folder to organize your routes logically (e.g., one file per module).

When a user accesses a URL, ORoute locates the path, runs filters, then instantiates the component and calls run(), passing a user defined DTO or a generic ORequest.


Defining Routes

To define a route, use the static methods of ORoute corresponding to the HTTP verbs: get(), post(), put(), or delete().

Basic Syntax

use Osumi\OsumiFramework\Routing\ORoute;
use Osumi\OsumiFramework\App\Module\Home\Index\IndexComponent;

ORoute::get('/', IndexComponent::class);

Route Parameters


Filters

Filters are classes executed before the main component. They are commonly used for authentication (checking tokens), logging, or request validation.

Docs: /docs/en/concepts/filters.md

use Osumi\OsumiFramework\App\Filter\LoginFilter;
use Osumi\OsumiFramework\App\Module\User\Profile\ProfileComponent;

ORoute::post('/profile', ProfileComponent::class, [LoginFilter::class]);


Grouping Routes

Osumi Framework provides three ways to group routes that share common characteristics:

1. Prefixes

Use prefixes when multiple routes share the same URL start (e.g., an API). Prefixes can be nested; each nested prefix is appended to the active prefix.

ORoute::prefix('/api', function(): void {
  ORoute::get('/health', HealthComponent::class);

  ORoute::prefix('/admin', function(): void {
    ORoute::post('/login', LoginComponent::class);
    ORoute::get('/me', MeComponent::class, [AdminAuthFilter::class]);
  });
});

This registers /api/health, /api/admin/login, and /api/admin/me.

2. Layouts

Used when multiple routes share the same visual structure (header, footer, etc.).

ORoute::layout(MainLayoutComponent::class, function() {
  ORoute::get('/home', HomeComponent::class);
  ORoute::get('/contact', ContactComponent::class);
});

3. Groups (Prefix + Layout)

Combines a prefix and layout assignment in a single block. Groups can be nested with other groups or prefixes; their URL prefixes accumulate, and each group applies its layout to the routes declared inside it.

ORoute::group('/admin', AdminLayoutComponent::class, function(): void {
  ORoute::group('/users', UserLayoutComponent::class, function(): void {
    ORoute::get('/profile', ProfileComponent::class);
  });
});

The route above is registered at /admin/users/profile and uses UserLayoutComponent.

URL Normalization

All static ORoute methods normalize URLs. Leading slashes are made consistent, repeated slashes are collapsed, and trailing slashes are removed except for the root URL /. This applies to get(), post(), put(), delete(), view(), group(), and prefix().

For example, nested prefixes with extra slashes:

ORoute::prefix('/api/', function(): void {
  ORoute::prefix('//admin///', function(): void {
    ORoute::get('//users/', UsersComponent::class);
  });
});

Register the route at /api/admin/users.


Static Views

If you need to serve a static file or a simple template without the logic of a full action component, use ORoute::view().

ORoute::view('/about-us', 'about-us.html');

Parameters on routes

URLs can be defined to have parameters on them using the :name syntax.

ORoute::get('/user/:id', UserComponent::class);
ORoute::get('/location/:name', LocationComponent::class);

The method run(ORequest $req) of the component can then access that parameter using methods such as getParamInt('id') or getParamString('name').


Summary of ORoute Methods

Method Description
get() Registers a GET route.
post() Registers a POST route.
put() Registers a PUT route.
delete() Registers a DELETE route.
view() Registers a route that renders a static file directly.
prefix() Groups routes under a cumulative, nestable URL prefix.
layout() Groups routes under a common layout component.
group() Groups routes with a nestable prefix and layout.

Best Practices