NAME
Gears::Controller - Base class for controllers
SYNOPSIS
package My::App::Controller::User;
use v5.40;
use Mooish::Base -standard;
extends 'Gears::Controller';
sub configure ($self)
{
my $r = $self->app->router;
$r->add('/user/:id');
}
# Later in your application
$app->load_controller('User');
DESCRIPTION
Gears::Controller is a base class for application controllers. Controllers are specialized Gears::Components that typically define routes and handle requests. They extend the component class without adding additional functionality, serving primarily as a semantic distinction for organizing request handlers.
Controllers are loaded using "load_controller" in Gears::App, which automatically resolves the controller class name relative to the application's namespace and instantiates it with a reference to the application.
Usage patterns
Route Definition
Controllers typically define their routes in the build method using "router" in Gears::App. Build method is not called automatically in subclasses, preventing routing from being accidentally repeated in a subclass which omitted the build method.
Accessing Configuration
Controllers can access application configuration through "config" in Gears::App object.
Persistent controllers
Controllers are generally supposed to be persistent - built only once at the start of the application and reused for every request. Implementations can chose to clone built controllers if necessary.
EXTENDING
You will want to create a base controller for your framework, for example:
package My::Framework::Controller;
use v5.40;
use Mooish::Base -standard;
extends 'Gears::Controller';
sub configure ($self)
{
# do something before build method is called
}
INTERFACE
Controllers inherit all attributes and methods from Gears::Component