This site is built with Viewi itself. It is experimental and still in development. If you see any bugs please do not hesitate and open an issue or DM me on Twitter.

Routing

Basics

Viewi has built in routing support. And if you are familiar with at least one the popular PHP frameworks, you will find it very familiar. So, let's dig into. First, you need to include use Viewi\Routing\Route;. And then, just define your routes and target components.

<?php

use Application\Components\Views\Home\HomePage;
use Application\Components\Views\NotFound\NotFoundPage;
use Viewi\Routing\Route;

Route::get('/', HomePage::class);
Route::all('*', NotFoundPage::class);

Available route methods

You can register your component under any HTTP verb and it will be executed accordingly once url pattern has been matched.

Route::get(string $url, string $component, ?array $defaults = null)
Route::post(string $url, string $component, ?array $defaults = null)
Route::put(string $url, string $component, ?array $defaults = null)
Route::delete(string $url, string $component, ?array $defaults = null)
Route::patch(string $url, string $component, ?array $defaults = null)
Route::options(string $url, string $component, ?array $defaults = null)
Route::all(string $url, string $component, ?array $defaults = null)

Route::add(string $method, string $url, $callable, ?array $defaults = null)

Route::add allows you to register not only Viewi component, but anything that is callable (function, [class, method], etc.)

Route parameters

{param} captures required segment from the URI and injects it into your component in place of parameter with the same name. Order doesn't matter. For example:

Route::get('users/{userId}', UserComponent::class);
...
class UserComponent extends BaseComponent
{
    function __init(SomeService $service, string $userId)
    {
        // 'use $userId here'
        ...
    }
}

{param?} captures optional segment from the URI and injects it into your component in place of parameter with the same name. In case if segment is empty, default or null value will be injected. Order doesn't matter. For example:

Route::get('posts/{name?}', PostsComponent::class);
...
class PostsComponent extends BaseComponent
{
    function __init(SomeService $service, ?string $name)
    {
        // 'use $name here, could be null'
        ...
    }
}

where sets constraint rules (regular expression constrains).

Route::get(
    'search/{search}',
    SearchPage::class
)->where('search', '.*');
...
class SearchPage extends BaseComponent
{
    function __init(SomeService $service, string $search)
    {
        // 'use $search here'
        ...
    }
}

Segments allowed to contain all characters except /. But you can explicitly allow any character by using where.

{param<RegEx>} sets constraint rules (regular expression constrains) without using where.

Route::get(
    '/category/{id<\\d+>}',
    CategoryPage::class
)->where('search', '.*');
...
class CategoryPage extends BaseComponent
{
    function __init(SomeService $service, int $id)
    {
        // 'use $id here'
        ...
    }
}

You are allowed to use all this rules together, for example:
/products/{type}/{query<[A-Za-z]+>?}
will match URI and catch required parameter type and optional parameter query only if it consists of letters. Otherwise this route will not be matched.

You are allowed to write your routes with or without leading slash. Both will be valid and will not affect route matching. /products/{type}
is equal to
products/{type}

Use * to catch any URI. Like
Route::all('*', NotFoundPage::class);
or
Route::all('list-*', ListNotFoundPage::class);
or
Route::get('/docs/*', DocsPage::class);
Just make sure to include these at the end, as they may intercept your other routes.

Default route parameters

Sometimes you want to guard parameters from empty values. You can do this by using default parameters.

Route::get('/blog/{page}', BlogPage::class, ['page' => 1]);