NAME
Minima::Controller - Base class for controllers used with Minima
SYNOPSIS
use Minima::Controller;
my $controller = Minima::Controller->new(
app => $app, # Minima::App
route => $match, # a match returned by Minima::Router
);
$controller->hello;
DESCRIPTION
Serving as a base class to controllers used with Minima, this class provides the basic infrastructure for any type of controller. It is built around Plack::Request and Plack::Response objects, allowing subclasses to interactly directly with Plack.
Minima::Controller also keeps references to the Minima::App and Plack environment. Additionally, it retains data received from the router, making it readily available to controllers.
This base class is not connected to any view, which is left to methods or subclasses. However, it sets a default Content-Type
header for the response as 'text/plain; charset=utf-8'
and response code to 200.
CONFIGURATION
The request_encoding
key can be included in the main Minima::App configuration hash to specify the expected request encoding. The GET and POST parameters will be decoded based on this setting.
If not set, request_encoding
defaults to UTF-8
. You may use any encoding value supported by Encode.
METHODS
new
method new (app, route = {})
Instantiates a controller with the given $app
reference, and optionally the hash reference returned by the router. If this hash reference contains data extracted from the URI by Minima::Router, then this data will be made available to the controller through the route
field.
redirect
method redirect ($url, $code = 302)
Utility method to set the redirect header to the given URL and code (defaults to 302, a temporary redirect) and finalize the response.
Use with return
inside other controller methods to shortcut:
# someone shouldn't be here
return $self->redirect('/login');
# continue for logged in users
render
method render ($view, $data = {})
Utility method to call render
on the passed view, together with optional data, and save to the response body. It then calls prepare_response
on the passed view and returns the finalized response.
EXTRAS
hello, not_found
Methods used to emit a minimal hello, world
or not found response.
print_env
Returns a plain text printout of the current Plack environment.
dd
method dd ($ref)
Sets the response to text/plain
, dumps the passed reference with Data::Dumper, and finalizes the response. Useful for debugging.
return dd($my_data);
ATTRIBUTES
All attributes below are accessible through reader methods.
env
-
Plack environment.
app
-
Reference to a Minima::App.
route
-
Hash reference returned by the router.
request
-
Internal Plack::Request
response
-
Internal Plack::Response
params
-
Decoded GET and POST parameters merged in a Hash::MultiValue. See "Configuration" to set the desired encoding.
SEE ALSO
Minima, Minima::App, Minima::Router, Minima::View, Plack::Request, Plack::Response, perlclass.
AUTHOR
Cesar Tessarin, <cesar@tessarin.com.br>.
Written in September 2024.