Tereta/Application Module

Overview

Application bootstrap module. Responsible for launching the framework in two modes: web (HTTP request handling) and CLI (console commands).

This is the entry point that loads configuration, registers all modules, and delegates control to the appropriate controller or command.

Web Application

Entry point: pub/index.php.

When processing an HTTP request, the application:

  1. Loads configuration from .config.php
  2. Registers all modules
  3. Resolves domain and route from URL
  4. Delegates the request to the appropriate controller
  5. Renders the result through the template system with layout support
Secure HTTP headers are applied automatically.

Controller Registration

To add a new web controller, create a class implementing the Controller interface and annotated with the #[ControllerAttribute]:

<?php

declare(strict_types=1);

namespace Tereta\YourModule\Controllers;

use Tereta\Route\Attributes\Controller as ControllerAttribute;
use Tereta\Route\Interfaces\Controller;
use Tereta\Route\Models\Response as ResponseModel;
use Tereta\Route\Models\Request as RequestModel;

#[ControllerAttribute(
alias: 'yourmodule/action',
match: '/^yourmodule\/action(?:\/(?<id>[0-9a-z]+))?$/Usi',
uri: 'yourmodule/action/%s',
method: 'get'
)]
class YourController implements Controller
{
public function __construct(protected RequestModel $requestModel)
{
}

public function handle(): ResponseModel
{
return ModelController::factory()->create([
'handler' => 'html',
'data' => $viewData,
'meta' => $metaData,
]);
}
}

#[ControllerAttribute] parameters:

  • alias — unique controller identifier (e.g., 'help', 'thread/view')
  • match — regex pattern for URL matching
  • uri — URI template for reverse routing (%s for parameter substitution)
  • method — HTTP method ('get' or 'post')
To handle multiple HTTP methods, add multiple attributes to the same class:
#[ControllerAttribute(alias: 'thread/view', match: '/^thread\/view\/(?<id>\d+)$/Ui', uri: 'thread/view/%s', method: 'get')]
#[ControllerAttribute(alias: 'thread/view/post', match: '/^thread\/view\/(?<id>\d+)$/Ui', uri: 'thread/view/%s', method: 'post')]
class View implements Controller
{
    // ...
}

No manual registration needed — the framework automatically discovers controllers via Composer DI and the #[ControllerAttribute].

CLI Application

Entry point: cli.php.

Usage:

./cli.php <command>

If no command is specified, the list command is executed, displaying all available commands.

Commands are automatically discovered from registered modules.

Command Registration

To add a new CLI command, create a class implementing the Command interface and annotated with #[CommandAttribute]:

<?php

declare(strict_types=1);

namespace Tereta\YourModule\Commands;

use Tereta\Cli\Attributes\Command as CommandAttribute;
use Tereta\Cli\Interfaces\Command;
use Tereta\Cli\Models\Input;
use Tereta\Cli\Services\Output;

#[CommandAttribute(name: 'your:command', description: 'Command description')]
class YourCommand implements Command
{
public function execute(Input $input): void
{
$argument = $input->getArgument(0); // positional argument
$option = $input->getOption('name'); // --name=value

Output::singleton()->info('Done!');
}
}

No manual registration needed — the framework automatically discovers the class via Composer DI and the #[CommandAttribute].

Configuration

Application configuration is defined in the .config.php file in the project root. Copy the template .config.php.example and edit it to suit your needs.

cp .config.php.example .config.php

The configuration includes global parameters as well as nested configurations for various modules such as Tereta/Page, Tereta/Db, Tereta/Customer and others:

<?php

declare(strict_types=1);

use Tereta\Core\Models\Value;

return Value::factory()->create()
->set('email', '{contact_email}') # Contact email
->set('smtp', Value::factory()->create() # SMTP settings for sending emails
->set('host', '{smtp_host') # SMTP server address
->set('username', '{smtp_username}') # SMTP username
->set('password', '{smtp_password}') # SMTP password
->set('from', '{smtp_from_email}')) # Sender email address
->set('page', Value::factory()->create()->set('location', '{page_location}')) # Tereta/Page module config, specifying the page files location (filesystem level)
->set('pdo', Value::factory()->create()->set('dsn', '{pdo_dsn}')) # Database connection via PDO, where {pdo_dsn} is the DSN connection string. See Tereta/Db module for details
->set('agreement', Value::factory()->create() # Tereta/Customer module config for user agreement
->set('document', '{agreement_document}') # Path to HTML/TXT document
->set('links.privacyPolicy', Value::factory()->create()->set('label', 'Privacy Policy')->set('location', '{privacy_policy_location}')) # Privacy policy link, where label is the link text and location is the URL or document path
->set('links.disclaimer', Value::factory()->create()->set('label', 'Disclaimer')->set('location', '{disclaimer_location}')) # Disclaimer link, where label is the link text and location is the URL or document path
->set('links.terms', Value::factory()->create()->set('label', 'Terms of Service')->set('location', '{terms_of_service_location}')) # Terms of service link, where label is the link text and location is the URL or document path
)
->set('googleAuthorisation', Value::factory()->create() # Tereta/Customer module config for Google authentication
->set('clientId', '{google_client_id}') # Client ID obtained from Google API Console
->set('clientSecret', '{google_client_secret}')) # Client secret obtained from Google API Console
->set('googleRecaptcha', Value::factory()->create() # Tereta/Customer module config for Google reCAPTCHA spam protection
->set('recaptchaSiteKey', '{recaptcha_site_key}') # Site key obtained from Google reCAPTCHA Admin Console
->set('recaptchaSecretKey', '{recaptcha_secret_key}')); # Secret key obtained from Google reCAPTCHA Admin Console

Error Handling

The application handles errors and displays user-friendly pages:

  • 404 — page or domain not found
  • 500 — internal server error
In development mode (display_errors = On), the error page additionally displays a stack trace for debugging.

Author and License

Author: Tereta Alexander
Website: tereta.dev
License: Apache License 2.0. See LICENSE.

 www.████████╗███████╗██████╗ ███████╗████████╗ █████╗
     ╚══██╔══╝██╔════╝██╔══██╗██╔════╝╚══██╔══╝██╔══██╗
        ██║   █████╗  ██████╔╝█████╗     ██║   ███████║
        ██║   ██╔══╝  ██╔══██╗██╔══╝     ██║   ██╔══██║
        ██║   ███████╗██║  ██║███████╗   ██║   ██║  ██║
        ╚═╝   ╚══════╝╚═╝  ╚═╝╚══════╝   ╚═╝   ╚═╝  ╚═╝
                                                      .dev

Copyright (c) 2024-2026 Tereta Alexander