NAME
Thunderhorse::Controller - Base controller class for Thunderhorse
SYNOPSIS
package MyApp::Controller::User;
use v5.40;
use Mooish::Base;
extends 'Thunderhorse::Controller';
sub build ($self)
{
$self->router->add('/user/:id' => { to => 'show' });
}
async sub show ($self, $ctx, $id)
{
await $ctx->res->text("User ID: $id");
}
DESCRIPTION
Thunderhorse::Controller is the base controller class for Thunderhorse applications. It extends Gears::Controller and provides core functionality for handling web requests, including routing, URL generation, and error handling.
Controllers are automatically loaded by the application when defined in configuration or loaded explicitly with "load_controller" in Thunderhorse::App.
INTERFACE
Inherits all interface from Gears::Controller and Gears::Component, and adds the interface documented below.
Attributes
No special attributes.
Methods
new
$object = $class->new(%args)
Standard Mooish constructor. Consult "Attributes" section for available constructor arguments.
loop
Delegated method for "loop" in Thunderhorse::App
config
Delegated method for "config" in Gears::App
router
$router = $self->router()
Returns the application router configured for this controller.
url_for
$url = $self->url_for($name, @args)
Generates a URL for a route named $name. @args (usually a list of key/value pairts) are passed to the route builder. Throws an exception if the route does not exist.
abs_url
$url = $self->abs_url($path = '')
Converts a relative path to an absolute URL using the app_url configuration value (https://localhost:5000 by default). $path can be omitted and will be treated as an empty string, returning the base absolute url of the application.
abs_url_for
$url = $self->abs_url_for($name, @args)
Convenience method which combines "url_for" and "abs_url" to generate an absolute URL for a named route.
render_error
$self->render_error($ctx, $code, $message = undef)
Renders an error response with the given HTTP status code. By default, it delegates to the application's "render_error" in Thunderhorse::App method.
render_response
$self->render_response($ctx, $result)
Renders a response from $result, which contains what was returned by the handler. By default, it delegates to the application's "render_response" in Thunderhorse::App method.
on_error
async sub on_error ($self, $ctx, $error) { ... }
Error hook called when an exception occurs during request processing. Can be overridden to customize error handling.