Mojolic...ntroller(3)

πŸ’… Mojolicious::Controller β€” the controller base class, periodt

Okay so this page is straight out of the User Contributed Perl Documentation (yes, the perldoc vibes), and it's about Mojolicious::Controller. Buckle up bestie, part 1 is a whole journey πŸŽ€


✨ SYNOPSIS ✨

  # Controller
  package MyApp::Controller::Foo;
  use Mojo::Base 'Mojolicious::Controller', -signatures;

  # Action
  sub bar ($self) {
    my $name = $self->param('name');
    $self->res->headers->cache_control('max-age=1, no-cache');
    $self->render(json => {hello => $name});
  }

🌸 DESCRIPTION 🌸

Mojolicious::Controller is the base class for your Mojolicious controllers. Main character energy, literally. It's also the default controller class β€” unless you go and set controller_class in Mojolicious, in which case, your call, your rules πŸ’…


πŸ¦‹ ATTRIBUTES πŸ¦‹

Mojolicious::Controller implements the following attributes. Yes, all of them, no skips.

🫢 app

  my $app = $c->app;
  $c      = $c->app(Mojolicious->new);

A reference back to the application that dispatched to this controller β€” usually a Mojolicious object. Heads up though: this attribute is weakened πŸ™ˆ (so don't expect it to be holding the app alive all by itself).

  # Use application logger
  $c->app->log->debug('Hello Mojo');

  # Generate path
  my $path = $c->app->home->child('templates', 'foo', 'bar.html.ep');

🎯 match

  my $m = $c->match;
  $c    = $c->match(Mojolicious::Routes::Match->new);

Router results for the current request. Defaults to a Mojolicious::Routes::Match object.

  # Introspect
  my $name   = $c->match->endpoint->name;
  my $foo    = $c->match->endpoint->pattern->defaults->{foo};
  my $action = $c->match->stack->[-1]{action};

πŸ“ tx

  my $tx = $c->tx;
  $c     = $c->tx(Mojo::Transaction::HTTP->new);

The transaction that's currently being processed β€” usually a Mojo::Transaction::HTTP or Mojo::Transaction::WebSocket object. And bestie, note that this attribute is weakened πŸ’€ So the object needs to be referenced somewhere else as well when you're doing non-blocking operations and the underlying connection might get closed early. No cap, that one bites people.

  # Check peer information
  my $address = $c->tx->remote_address;
  my $port    = $c->tx->remote_port;

  # Increase size limit for WebSocket messages to 16MiB
  $c->tx->max_websocket_size(16777216) if $c->tx->is_websocket;

  # Perform non-blocking operation without knowing the connection status
  my $tx = $c->tx;
  Mojo::IOLoop->timer(2 => sub {
    $c->app->log->debug($tx->is_finished ? 'Finished' : 'In progress');
  });

πŸ’œ METHODS πŸ’œ

Mojolicious::Controller inherits all methods from Mojo::Base and implements the following new ones. Let's go πŸ‘‘

🌈 continue

  $c->continue;

Continue the dispatch chain from an intermediate destination with continue in Mojolicious::Routes.

  my $value = $c->cookie('foo');
  $c        = $c->cookie(foo => 'bar');
  $c        = $c->cookie(foo => 'bar', {path => '/'});

Access request cookie values and create new response cookies. If there are multiple values sharing the same name, and you want to access more than just the last one, you can use every_cookie.

  # Create response cookie with domain and expiration date
  $c->cookie(user => 'sri', {domain => 'example.com', expires => time + 60});

  # Create secure response cookie
  $c->cookie(secret => 'I <3 Mojolicious', {secure => 1, httponly => 1});
  my $value = $c->encrypted_cookie('foo');
  $c        = $c->encrypted_cookie(foo => 'bar');
  $c        = $c->encrypted_cookie(foo => 'bar', {path => '/'});

Access encrypted request cookie values and create new encrypted response cookies. If there are multiple values sharing the same name, and you want more than just the last one, hit up every_encrypted_cookie. Cookies are encrypted with ChaCha20-Poly1305, to prevent tampering, and the ones failing decryption will be automatically discarded. Security icon behaviour, honestly πŸ›‘οΈ

  my $values = $c->every_cookie('foo');

Similar to cookie, but returns all request cookie values sharing the same name as an array reference.

  $ Get first cookie value
  my $first = $c->every_cookie('foo')->[0];
  my $values = $c->every_encrypted_cookie('foo');

Similar to encrypted_cookie, but returns all encrypted request cookie values sharing the same name as an array reference.

  # Get first encrypted cookie value
  my $first = $c->every_encrypted_cookie('foo')->[0];

πŸ“¦ every_param

  my $values = $c->every_param('foo');

Similar to param, but returns all values sharing the same name as an array reference.

  # Get first value
  my $first = $c->every_param('foo')->[0];
  my $values = $c->every_signed_cookie('foo');

Similar to signed_cookie, but returns all signed request cookie values sharing the same name as an array reference.

  # Get first signed cookie value
  my $first = $c->every_signed_cookie('foo')->[0];

πŸ‘‹ finish

  $c = $c->finish;
  $c = $c->finish(1000);
  $c = $c->finish(1003 => 'Cannot accept data!');
  $c = $c->finish('Bye!');

Close a WebSocket connection or long poll stream gracefully. This method will automatically respond to WebSocket handshake requests with a 101 response status, to establish the WebSocket connection. Polite and functional, we love to see it 🫢

🧰 helpers

  my $helpers = $c->helpers;

Return a proxy object containing the current controller object and on which helpers provided by app can be called. This includes all helpers from Mojolicious::Plugin::DefaultHelpers and Mojolicious::Plugin::TagHelpers.

  # Make sure to use the "title" helper and not the controller method
  $c->helpers->title('Welcome!');

  # Use a nested helper instead of the "reply" controller method
  $c->helpers->reply->not_found;

πŸ“‘ on

  my $cb = $c->on(finish => sub {...});

Subscribe to events of tx, which is usually a Mojo::Transaction::HTTP or Mojo::Transaction::WebSocket object. This method will automatically respond to WebSocket handshake requests with a 101 response status, to establish the WebSocket connection. Same deal as finish, bestie.

  # Do something after the transaction has been finished
  $c->on(finish => sub ($c) {
    $c->app->log->debug('All data has been sent');
  });

  # Receive WebSocket message
  $c->on(message => sub ($c, $msg) {
    $c->app->log->debug("Message: $msg");
  });

  # Receive JSON object via WebSocket message
  $c->on(json => sub ($c, $hash) {
    $c->app->log->debug("Test: $hash->{test}");
  });

  # Receive WebSocket "Binary" message
  $c->on(binary => sub ($c, $bytes) {
    my $len = length $bytes;
    $c->app->log->debug("Received $len bytes");
  });

πŸŽ€ param

  my $value = $c->param('foo');
  $c        = $c->param(foo => 'ba;r');
  $c        = $c->param(foo => 'ba;r', 'baz');
  $c        = $c->param(foo => ['ba;r', 'baz']);

Access route placeholder values that are not reserved stash values, file uploads, as well as GET and POST parameters extracted from the query string and application/x-www-form-urlencoded or multipart/form-data message body β€” in that order. If there are multiple values sharing the same name, and you want more than just the last one, use every_param.

⚠️ Parts of the request body need to be loaded into memory to parse POST parameters, so you have to make sure it is not excessively large. There's a 16MiB limit for requests by default. Respect the limit, bestie 😭
  # Get first value
  my $first = $c->every_param('foo')->[0];

For more control you can also access request information directly. Very choose-your-own-adventure of you:

  # Only GET parameters
  my $foo = $c->req->query_params->param('foo');

  # Only POST parameters
  my $foo = $c->req->body_params->param('foo');

  # Only GET and POST parameters
  my $foo = $c->req->param('foo');

  # Only file uploads
  my $foo = $c->req->upload('foo');

🎨 render

  my $bool = $c->render;
  my $bool = $c->render(foo => 'bar', baz => 23);
  my $bool = $c->render(template => 'foo/index');
  my $bool = $c->render(template => 'index', format => 'html');
  my $bool = $c->render(data => $bytes);
  my $bool = $c->render(text => 'Hello!');
  my $bool = $c->render(json => {foo => 'bar'});
  my $bool = $c->render(handler => 'something');
  my $bool = $c->render('foo/index');

Render content with renderer in Mojolicious and emit hooks before_render in Mojolicious as well as after_render in Mojolicious β€” or dies if no response could be generated πŸ’€ All additional key/value pairs get merged into the stash.

  # Render characters
  $c->render(text => 'I β™₯ Mojolicious!');

  # Render characters (alternative)
  $c->stash(text => 'I β™₯ Mojolicious!')->render;

  # Render binary data
  use Mojo::JSON qw(encode_json);
  $c->render(data => encode_json({test => 'I β™₯ Mojolicious!'}));

  # Render JSON
  $c->render(json => {test => 'I β™₯ Mojolicious!'});

  # Render inline template
  $c->render(inline => '<%= 1 + 1 %>');

  # Render template "foo/bar.html.ep"
  $c->render(template => 'foo/bar', format => 'html', handler => 'ep');

  # Render template "test.*.*" with arbitrary values "foo" and "bar"
  $c->render(template => 'test', foo => 'test', bar => 23);

  # Render template "test.xml.*"
  $c->render(template => 'test', format => 'xml');

  # Render template "test.xml.*" (alternative)
  $c->render('test', format => 'xml');

⏳ render_later

  $c = $c->render_later;

Disable automatic rendering to delay response generation β€” only necessary if automatic rendering would result in a response. iykyk, you reach for this when you're about to do something async and don't want the framework jumping the gun.

  # Delayed rendering
  $c->render_later;
  Mojo::IOLoop->timer(2 => sub { $c->render(text => 'Delayed by 2 seconds!') });

πŸ€” render_maybe

  my $bool = $c->render_maybe;
  my $bool = $c->render_maybe(foo => 'bar', baz => 23);
  my $bool = $c->render_maybe('foo/index', format => 'html');

Try to render content, but do not call reply->not_found in Mojolicious::Plugin::DefaultHelpers if no response could be generated. All arguments get localized automatically and are only available during this render operation. Takes the same arguments as render.

  # Render template "index_local" only if it exists
  $c->render_maybe('index_local') or $c->render('index');

πŸͺ„ render_to_string

  my $output = $c->render_to_string('foo/index', format => 'pdf');

Try to render content and return it wrapped in a Mojo::ByteStream object, or return undef. All arguments get localized automatically and are only available during this render operation. Takes the same arguments as render.

  # Render inline template
  my $two = $c->render_to_string(inline => '<%= 1 + 1 %>');

βœ… rendered

  $c = $c->rendered;
  $c = $c->rendered(302);

Finalize the response and emit hook after_dispatch in Mojolicious. Defaults to using a 200 response code.

  # Custom response
  $c->res->headers->content_type('text/plain');
  $c->res->body('Hello World!');
  $c->rendered(200);

πŸ“¨ req

  my $req = $c->req;

Get the Mojo::Message::Request object from tx.


okay that's where part 1 wraps up, bestie β€” the rest of the methods are in part 2, don't go anywhere πŸ’–

continuing the req flex, here's the longer version:

# Longer version
my $req = $c->tx->req;

# Extract request information
my $id     = $c->req->request_id;
my $method = $c->req->method;
my $url    = $c->req->url->to_abs;
my $info   = $c->req->url->to_abs->userinfo;
my $host   = $c->req->url->to_abs->host;
my $agent  = $c->req->headers->user_agent;
my $custom = $c->req->headers->header('Custom-Header');
my $bytes  = $c->req->body;
my $str    = $c->req->text;
my $hash   = $c->req->params->to_hash;
my $all    = $c->req->uploads;
my $value  = $c->req->json;
my $foo    = $c->req->json('/23/foo');
my $dom    = $c->req->dom;
my $bar    = $c->req->dom('div.bar')->first->text;

res πŸŽ€

my $res = $c->res;

Grab the Mojo::Message::Response object from tx. slay.

# Longer version
my $res = $c->tx->res;

# Force file download by setting a response header
$c->res->headers->content_disposition('attachment; filename=foo.png;');

# Use a custom response header
$c->res->headers->header('Custom-Header' => 'whatever');

# Make sure response is cached correctly
$c->res->headers->cache_control('public, max-age=300');
$c->res->headers->append(Vary => 'Accept-Encoding');

send πŸ’…

$c = $c->send({binary => $bytes});
$c = $c->send({text   => $bytes});
$c = $c->send({json   => {test => [1, 2, 3]}});
$c = $c->send([$fin, $rsv1, $rsv2, $rsv3, $op, $payload]);
$c = $c->send($chars);
$c = $c->send($chars => sub ($c) {...});

Send a message or frame non-blocking via WebSocket, bestie. The optional drain callback gets executed once all the data has been written. This method will automatically respond to WebSocket handshake requests with a 101 response status, so the WebSocket connection gets established. iconic.

# Send "Text" message
$c->send('I β™₯ Mojolicious!');

# Send JSON object as "Text" message
$c->send({json => {test => 'I β™₯ Mojolicious!'}});

# Send JSON object as "Binary" message
use Mojo::JSON qw(encode_json);
$c->send({binary => encode_json({test => 'I β™₯ Mojolicious!'})});

# Send "Ping" frame
use Mojo::WebSocket qw(WS_PING);
$c->send([1, 0, 0, 0, WS_PING, 'Hello World!']);

# Make sure the first message has been written before continuing
$c->send('First message!' => sub ($c) { $c->send('Second message!') });

For mostly idle WebSockets you might also want to increase the inactivity timeout with "inactivity_timeout" in Mojolicious::Plugin::DefaultHelpers, which usually defaults to 30 seconds. no cap.

# Increase inactivity timeout for connection to 300 seconds
$c->inactivity_timeout(300);

session πŸ“

my $session = $c->session;
my $foo     = $c->session('foo');
$c          = $c->session({foo => 'bar'});
$c          = $c->session(foo => 'bar');

Persistent data storage for the next few requests. All session data gets serialized with Mojo::JSON and stored Base64 encoded in HMAC-SHA256 signed cookies, to prevent tampering. Note that cookies usually have a 4096 byte (4KiB) limit, depending on browser. keep that in mind fr fr.

# Manipulate session
$c->session->{foo} = 'bar';
my $foo = $c->session->{foo};
delete $c->session->{foo};

# Expiration date in seconds from now (persists between requests)
$c->session(expiration => 604800);

# Expiration date as absolute epoch time (only valid for one request)
$c->session(expires => time + 604800);

# Delete whole session by setting an expiration date in the past
$c->session(expires => 1);
my $value = $c->signed_cookie('foo');
$c        = $c->signed_cookie(foo => 'bar');
$c        = $c->signed_cookie(foo => 'bar', {path => '/'});

Access signed request cookie values and create new signed response cookies. If there are multiple values sharing the same name, and you want to access more than just the last one, you can use "every_signed_cookie". Cookies are cryptographically signed with HMAC-SHA256, to prevent tampering, and the ones failing signature verification will be automatically discarded. the way that works is lowkey genius.

stash 🌸

my $hash = $c->stash;
my $foo  = $c->stash('foo');
$c       = $c->stash({foo => 'bar', baz => 23});
$c       = $c->stash(foo => 'bar', baz => 23);

Non-persistent data storage and exchange for the current request. Application wide default values can be set with "defaults" in Mojolicious. Some stash values have a special meaning and are reserved, the full list is currently "action", "app", "cb", "controller", "data", "extends", "format", "handler", "inline", "json", "layout", "namespace", "path", "status", "template", "text" and "variant". Note that all stash values with a "mojo.*" prefix are reserved for internal use. don't touch those, bestie.

# Remove value
my $foo = delete $c->stash->{foo};

# Assign multiple values at once
$c->stash(foo => 'test', bar => 23);

url_for πŸ‘‘

my $url = $c->url_for;
my $url = $c->url_for(name => 'sebastian');
my $url = $c->url_for({name => 'sebastian'});
my $url = $c->url_for('test', name => 'sebastian');
my $url = $c->url_for('test', {name => 'sebastian'});
my $url = $c->url_for('/index.html');
my $url = $c->url_for('//example.com/index.html');
my $url = $c->url_for('http://example.com/index.html');
my $url = $c->url_for('mailto:sri@example.com');
my $url = $c->url_for('#whatever');

Generate a portable Mojo::URL object with base for a path, URL or route. periodt.

# Rebuild URL for the current route
$c->url_for;

# Rebuild URL for the current route, but replace the "name" placeholder value
$c->url_for(name => 'sebastian');

# Absolute URL for the current route
$c->url_for->to_abs;

# Build URL for route "test" with two placeholder values
$c->url_for('test', name => 'sebastian', foo => 'bar');

# "http://127.0.0.1:3000/index.html" if application was started with Morbo
$c->url_for('/index.html')->to_abs;

# "https://127.0.0.1:443/index.html" if application was started with Morbo
$c->url_for('/index.html')->to_abs->scheme('https')->port(443);

# "/index.html?foo=bar" if application is deployed under "/"
$c->url_for('/index.html')->query(foo => 'bar');

# "/myapp/index.html?foo=bar" if application is deployed under "/myapp"
$c->url_for('/index.html')->query(foo => 'bar');

You can also use the helper "url_with" in Mojolicious::Plugin::DefaultHelpers to inherit query parameters from the current request. cute.

# "/list?q=mojo&page=2" if current request was for "/list?q=mojo&page=1"
$c->url_with->query({page => 2});

url_for_asset πŸ¦‹

my $url = $c->url_for_asset('/app.js');

Generate a portable Mojo::URL object with base for a static asset. simple as that.

url_for_file πŸ’œ

my $url = $c->url_for_file('/index.html');

Generate a portable Mojo::URL object with base for a static file. slay.

write πŸ”₯

$c = $c->write;
$c = $c->write('');
$c = $c->write($bytes);
$c = $c->write($bytes => sub ($c) {...});

Write dynamic content non-blocking. The optional drain callback will be executed once all data has been written. Calling this method without a chunk of data will finalize the response headers and allow for dynamic content to be written later. okay so this is the real main character energy.

# Keep connection alive (with Content-Length header)
$c->res->headers->content_length(6);
$c->write('Hel' => sub ($c) { $c->write('lo!') });

# Close connection when finished (without Content-Length header)
$c->write('Hel' => sub ($c) {
  $c->write('lo!' => sub ($c) { $c->finish });
});

You can call "finish" or write an empty chunk of data at any time to end the stream. no cap.

HTTP/1.1 200 OK
Date: Sat, 13 Sep 2014 16:48:29 GMT
Content-Length: 6
Server: Mojolicious (Perl)

Hello!

HTTP/1.1 200 OK
Connection: close
Date: Sat, 13 Sep 2014 16:48:29 GMT
Server: Mojolicious (Perl)

Hello!

For Comet (long polling) you might also want to increase the inactivity timeout with "inactivity_timeout" in Mojolicious::Plugin::DefaultHelpers, which usually defaults to 30 seconds. iykyk.

# Increase inactivity timeout for connection to 300 seconds
$c->inactivity_timeout(300);

write_chunk ✨

$c = $c->write_chunk;
$c = $c->write_chunk('');
$c = $c->write_chunk($bytes);
$c = $c->write_chunk($bytes => sub ($c) {...});

Write dynamic content non-blocking with chunked transfer encoding. The optional drain callback will be executed once all data has been written. Calling this method without a chunk of data will finalize the response headers and allow for dynamic content to be written later. let's gooo.

# Make sure previous chunk has been written before continuing
$c->write_chunk('H' => sub ($c) {
  $c->write_chunk('ell' => sub ($c) { $c->finish('o!') });
});

You can call "finish" or write an empty chunk of data at any time to end the stream. the vibes are immaculate.

HTTP/1.1 200 OK
Date: Sat, 13 Sep 2014 16:48:29 GMT
Transfer-Encoding: chunked
Server: Mojolicious (Perl)

1
H
3
ell
2
o!
0

write_sse 🌈

$c = $c->write_sse;
$c = $c->write_sse($event);
$c = $c->write_sse($event => sub ($c) {...});

Write Server-Sent Event (SSE) non-blocking. The optional drain callback will be executed once all data has been written. Calling this method without an event will finalize the response headers and allow for events to be written later. Note that this method is EXPERIMENTAL and might change without warning! bestie consider yourself warned πŸ’€.

# Send event with default "message" type
$c->write_sse({text => 'Hello World!'});

# You can send comments in regular intervals to keep the connection alive
$c->write_sse({comment => 'Keep connection alive!'});

# Make sure previous event has been written before continuing (id and type are optional)
$c->write_sse({id => 23, type => 'greeting', text => 'Hello World!'} => sub ($c) {
  $c->write_sse({id => 24, type => 'farewell', text => 'Goodbye World!'} => sub ($c) { $c->finish });
});

✨ HELPERS ✨

In addition to the "ATTRIBUTES" and "METHODS" above you can also call helpers provided by "app" on Mojolicious::Controller objects. This includes all helpers from Mojolicious::Plugin::DefaultHelpers and Mojolicious::Plugin::TagHelpers. obsessed.

# Call helpers
$c->layout('green');
$c->title('Welcome!');

# Longer version
$c->helpers->layout('green');

πŸ’– SEE ALSO πŸ’–

xoxo, your fave manpage πŸ’… this tea is from perl v5.42.0, dated 2026-07-15, and the page is Mojolic...ntroller(3). periodt 🫢

πŸ“¦ where this page comes from