.\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.16) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .ie \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .el \{\ . de IX .. .\} .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Mojolicious::Controller 3pm" .TH Mojolicious::Controller 3pm "2012-09-05" "perl v5.14.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Mojolicious::Controller \- Controller base class .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Mojo::Base \*(AqMojolicious::Controller\*(Aq; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Mojolicious::Controller is the base class for your Mojolicious controllers. It is also the default controller class for Mojolicious unless you set \f(CW\*(C`controller_class\*(C'\fR in your application. .SH "ATTRIBUTES" .IX Header "ATTRIBUTES" Mojolicious::Controller inherits all attributes from Mojo::Base and implements the following new ones. .ie n .SS """app""" .el .SS "\f(CWapp\fP" .IX Subsection "app" .Vb 2 \& my $app = $c\->app; \& $c = $c\->app(Mojolicious\->new); .Ve .PP A reference back to the application that dispatched to this controller, defaults to a Mojolicious object. .PP .Vb 2 \& # Use application logger \& $c\->app\->log\->debug(\*(AqHello Mojo!\*(Aq); .Ve .ie n .SS """match""" .el .SS "\f(CWmatch\fP" .IX Subsection "match" .Vb 2 \& my $m = $c\->match; \& $c = $c\->match(Mojolicious::Routes::Match\->new); .Ve .PP Router results for the current request, defaults to a Mojolicious::Routes::Match object. .PP .Vb 2 \& # Introspect \& my $foo = $c\->match\->endpoint\->pattern\->defaults\->{foo}; .Ve .ie n .SS """tx""" .el .SS "\f(CWtx\fP" .IX Subsection "tx" .Vb 2 \& my $tx = $c\->tx; \& $c = $c\->tx(Mojo::Transaction::HTTP\->new); .Ve .PP The transaction that is currently being processed, usually a Mojo::Transaction::HTTP or Mojo::Transaction::WebSocket object. .PP .Vb 2 \& # Check peer information \& my $address = $c\->tx\->remote_address; .Ve .SH "METHODS" .IX Header "METHODS" Mojolicious::Controller inherits all methods from Mojo::Base and implements the following new ones. .ie n .SS """cookie""" .el .SS "\f(CWcookie\fP" .IX Subsection "cookie" .Vb 4 \& my $value = $c\->cookie(\*(Aqfoo\*(Aq); \& my @values = $c\->cookie(\*(Aqfoo\*(Aq); \& $c = $c\->cookie(foo => \*(Aqbar\*(Aq); \& $c = $c\->cookie(foo => \*(Aqbar\*(Aq, {path => \*(Aq/\*(Aq}); .Ve .PP Access request cookie values and create new response cookies. .PP .Vb 2 \& # Create response cookie with domain \& $c\->cookie(name => \*(Aqsebastian\*(Aq, {domain => \*(Aqmojolicio.us\*(Aq}); .Ve .ie n .SS """finish""" .el .SS "\f(CWfinish\fP" .IX Subsection "finish" .Vb 2 \& $c\->finish; \& $c\->finish(\*(AqBye!\*(Aq); .Ve .PP Gracefully end WebSocket connection or long poll stream. .ie n .SS """flash""" .el .SS "\f(CWflash\fP" .IX Subsection "flash" .Vb 3 \& my $foo = $c\->flash(\*(Aqfoo\*(Aq); \& $c = $c\->flash({foo => \*(Aqbar\*(Aq}); \& $c = $c\->flash(foo => \*(Aqbar\*(Aq); .Ve .PP Data storage persistent only for the next request, stored in the \f(CW\*(C`session\*(C'\fR. .PP .Vb 3 \& # Show message after redirect \& $c\->flash(message => \*(AqUser created successfully!\*(Aq); \& $c\->redirect_to(\*(Aqshow_user\*(Aq, id => 23); .Ve .ie n .SS """on""" .el .SS "\f(CWon\fP" .IX Subsection "on" .Vb 1 \& my $cb = $c\->on(finish => sub {...}); .Ve .PP Subscribe to events of \f(CW\*(C`tx\*(C'\fR, which is usually a Mojo::Transaction::HTTP or Mojo::Transaction::WebSocket object. .PP .Vb 5 \& # Emitted when the transaction has been finished \& $c\->on(finish => sub { \& my $c = shift; \& $c\->app\->log\->debug(\*(AqWe are done!\*(Aq); \& }); \& \& # Emitted when new WebSocket messages arrive \& $c\->on(message => sub { \& my ($c, $message) = @_; \& $c\->app\->log\->debug("Message: $message"); \& }); .Ve .ie n .SS """param""" .el .SS "\f(CWparam\fP" .IX Subsection "param" .Vb 5 \& my @names = $c\->param; \& my $foo = $c\->param(\*(Aqfoo\*(Aq); \& my @foo = $c\->param(\*(Aqfoo\*(Aq); \& $c = $c\->param(foo => \*(Aqba;r\*(Aq); \& $c = $c\->param(foo => qw(ba;r ba;z)); .Ve .PP Access \s-1GET/POST\s0 parameters, file uploads and route captures that are not reserved stash values. .PP .Vb 2 \& # Only GET parameters \& my $foo = $c\->req\->url\->query\->param(\*(Aqfoo\*(Aq); \& \& # Only GET and POST parameters \& my $foo = $c\->req\->param(\*(Aqfoo\*(Aq); \& \& # Only file uploads \& my $foo = $c\->req\->upload(\*(Aqfoo\*(Aq); .Ve .ie n .SS """redirect_to""" .el .SS "\f(CWredirect_to\fP" .IX Subsection "redirect_to" .Vb 4 \& $c = $c\->redirect_to(\*(Aqnamed\*(Aq); \& $c = $c\->redirect_to(\*(Aqnamed\*(Aq, foo => \*(Aqbar\*(Aq); \& $c = $c\->redirect_to(\*(Aq/path\*(Aq); \& $c = $c\->redirect_to(\*(Aqhttp://127.0.0.1/foo/bar\*(Aq); .Ve .PP Prepare a \f(CW302\fR redirect response, takes the exact same arguments as \&\f(CW\*(C`url_for\*(C'\fR. .PP .Vb 2 \& # Conditional redirect \& return $c\->redirect_to(\*(Aqlogin\*(Aq) unless $c\->session(\*(Aquser\*(Aq); \& \& # Moved permanently \& $c\->res\->code(301); \& $c\->redirect_to(\*(Aqsome_route\*(Aq); .Ve .ie n .SS """render""" .el .SS "\f(CWrender\fP" .IX Subsection "render" .Vb 11 \& my $success = $c\->render; \& my $success = $c\->render(controller => \*(Aqfoo\*(Aq, action => \*(Aqbar\*(Aq); \& my $success = $c\->render({controller => \*(Aqfoo\*(Aq, action => \*(Aqbar\*(Aq}); \& my $success = $c\->render(template => \*(Aqfoo/index\*(Aq); \& my $success = $c\->render(template => \*(Aqindex\*(Aq, format => \*(Aqhtml\*(Aq); \& my $success = $c\->render(data => $bytes); \& my $success = $c\->render(text => \*(AqHello!\*(Aq); \& my $success = $c\->render(json => {foo => \*(Aqbar\*(Aq}); \& my $success = $c\->render(handler => \*(Aqsomething\*(Aq); \& my $success = $c\->render(\*(Aqfoo/index\*(Aq); \& my $output = $c\->render(\*(Aqfoo/index\*(Aq, partial => 1); .Ve .PP This is a wrapper around \*(L"render\*(R" in Mojolicious::Renderer exposing pretty much all functionality provided by it. It will set a default template to use based on the controller and action name or fall back to the route name. You can call it with a hash or hash reference of options which can be preceded by an optional template name. .ie n .SS """render_content""" .el .SS "\f(CWrender_content\fP" .IX Subsection "render_content" .Vb 4 \& my $output = $c\->render_content; \& my $output = $c\->render_content(\*(Aqheader\*(Aq); \& my $output = $c\->render_content(header => \*(AqHello world!\*(Aq); \& my $output = $c\->render_content(header => sub { \*(AqHello world!\*(Aq }); .Ve .PP Contains partial rendered templates, used for the renderers \f(CW\*(C`layout\*(C'\fR and \&\f(CW\*(C`extends\*(C'\fR features. .ie n .SS """render_data""" .el .SS "\f(CWrender_data\fP" .IX Subsection "render_data" .Vb 2 \& $c\->render_data($bytes); \& $c\->render_data($bytes, format => \*(Aqpng\*(Aq); .Ve .PP Render the given content as raw bytes, similar to \f(CW\*(C`render_text\*(C'\fR but data will not be encoded. .ie n .SS """render_exception""" .el .SS "\f(CWrender_exception\fP" .IX Subsection "render_exception" .Vb 2 \& $c\->render_exception(\*(AqOops!\*(Aq); \& $c\->render_exception(Mojo::Exception\->new(\*(AqOops!\*(Aq)); .Ve .PP Render the exception template \f(CW\*(C`exception.$mode.$format.*\*(C'\fR or \&\f(CW\*(C`exception.$format.*\*(C'\fR and set the response status code to \f(CW500\fR. .ie n .SS """render_json""" .el .SS "\f(CWrender_json\fP" .IX Subsection "render_json" .Vb 2 \& $c\->render_json({foo => \*(Aqbar\*(Aq}); \& $c\->render_json([1, 2, \-3], status => 201); .Ve .PP Render a data structure as \s-1JSON\s0. .ie n .SS """render_later""" .el .SS "\f(CWrender_later\fP" .IX Subsection "render_later" .Vb 1 \& $c\->render_later; .Ve .PP Disable automatic rendering, especially for long polling this can be quite useful. .PP .Vb 5 \& # Delayed rendering \& $c\->render_later; \& Mojo::IOLoop\->timer(2 => sub { \& $c\->render(text => \*(AqDelayed by 2 seconds!\*(Aq); \& }); .Ve .ie n .SS """render_not_found""" .el .SS "\f(CWrender_not_found\fP" .IX Subsection "render_not_found" .Vb 1 \& $c\->render_not_found; .Ve .PP Render the not found template \f(CW\*(C`not_found.$mode.$format.*\*(C'\fR or \&\f(CW\*(C`not_found.$format.*\*(C'\fR and set the response status code to \f(CW404\fR. .ie n .SS """render_partial""" .el .SS "\f(CWrender_partial\fP" .IX Subsection "render_partial" .Vb 3 \& my $output = $c\->render_partial(\*(Aqmenubar\*(Aq); \& my $output = $c\->render_partial(\*(Aqmenubar\*(Aq, format => \*(Aqtxt\*(Aq); \& my $output = $c\->render_partial(template => \*(Aqmenubar\*(Aq); .Ve .PP Same as \f(CW\*(C`render\*(C'\fR but returns the rendered result. .ie n .SS """render_static""" .el .SS "\f(CWrender_static\fP" .IX Subsection "render_static" .Vb 2 \& my $success = $c\->render_static(\*(Aqimages/logo.png\*(Aq); \& my $success = $c\->render_static(\*(Aq../lib/MyApp.pm\*(Aq); .Ve .PP Render a static file using \*(L"serve\*(R" in Mojolicious::Static, usually from the \&\f(CW\*(C`public\*(C'\fR directory or \f(CW\*(C`DATA\*(C'\fR section of your application. .ie n .SS """render_text""" .el .SS "\f(CWrender_text\fP" .IX Subsection "render_text" .Vb 2 \& $c\->render_text(\*(AqHello World!\*(Aq); \& $c\->render_text(\*(AqHello World\*(Aq, layout => \*(Aqgreen\*(Aq); .Ve .PP Render the given content as Perl characters, which will be encoded to bytes. See \f(CW\*(C`render_data\*(C'\fR for an alternative without encoding. Note that this does not change the content type of the response, which is \&\f(CW\*(C`text/html;charset=UTF\-8\*(C'\fR by default. .PP .Vb 2 \& # Render "text/plain" response \& $c\->render_text(\*(AqHello World!\*(Aq, format => \*(Aqtxt\*(Aq); .Ve .ie n .SS """rendered""" .el .SS "\f(CWrendered\fP" .IX Subsection "rendered" .Vb 2 \& $c = $c\->rendered; \& $c = $c\->rendered(302); .Ve .PP Finalize response and run \f(CW\*(C`after_dispatch\*(C'\fR plugin hook. .PP .Vb 4 \& # Stream content directly from file \& $c\->res\->content\->asset(Mojo::Asset::File\->new(path => \*(Aq/etc/passwd\*(Aq)); \& $c\->res\->headers\->content_type(\*(Aqtext/plain\*(Aq); \& $c\->rendered(200); .Ve .ie n .SS """req""" .el .SS "\f(CWreq\fP" .IX Subsection "req" .Vb 1 \& my $req = $c\->req; .Ve .PP Alias for \f(CW\*(C`$c\->tx\->req\*(C'\fR. Usually refers to a Mojo::Message::Request object. .PP .Vb 2 \& # Extract request information \& my $userinfo = $c\->req\->url\->userinfo; .Ve .ie n .SS """res""" .el .SS "\f(CWres\fP" .IX Subsection "res" .Vb 1 \& my $res = $c\->res; .Ve .PP Alias for \f(CW\*(C`$c\->tx\->res\*(C'\fR. Usually refers to a Mojo::Message::Response object. .PP .Vb 2 \& # Force file download by setting a custom response header \& $c\->res\->headers\->content_disposition(\*(Aqattachment; filename=foo.png;\*(Aq); .Ve .ie n .SS """respond_to""" .el .SS "\f(CWrespond_to\fP" .IX Subsection "respond_to" .Vb 5 \& $c\->respond_to( \& json => {json => {message => \*(AqWelcome!\*(Aq}}, \& html => {template => \*(Aqwelcome\*(Aq}, \& any => sub {...} \& ); .Ve .PP Automatically select best possible representation for resource from \f(CW\*(C`Accept\*(C'\fR request header, \f(CW\*(C`format\*(C'\fR stash value or \f(CW\*(C`format\*(C'\fR \s-1GET/POST\s0 parameter, defaults to rendering an empty \f(CW204\fR response. Unspecific \f(CW\*(C`Accept\*(C'\fR request headers that contain more than one \s-1MIME\s0 type are currently ignored, since browsers often don't really know what they actually want. .PP .Vb 5 \& $c\->respond_to( \& json => sub { $c\->render_json({just => \*(Aqworks\*(Aq}) }, \& xml => {text => \*(Aqworks\*(Aq}, \& any => {data => \*(Aq\*(Aq, status => 204} \& ); .Ve .ie n .SS """send""" .el .SS "\f(CWsend\fP" .IX Subsection "send" .Vb 5 \& $c = $c\->send({binary => $bytes}); \& $c = $c\->send({text => $bytes}); \& $c = $c\->send([$fin, $rsv1, $rsv2, $rsv3, $op, $payload]); \& $c = $c\->send(\*(AqHi there!\*(Aq); \& $c = $c\->send(\*(AqHi there!\*(Aq, sub {...}); .Ve .PP Send message or frame non-blocking via WebSocket, the optional drain callback will be invoked once all data has been written. .PP .Vb 2 \& # Send JSON object as text frame \& $c\->send({text => Mojo::JSON\->new\->encode({hello => \*(Aqworld\*(Aq})}); \& \& # Send "Ping" frame \& $c\->send([1, 0, 0, 0, 9, \*(AqHello World!\*(Aq]); .Ve .PP For mostly idle WebSockets you might also want to increase the inactivity timeout, which usually defaults to \f(CW15\fR seconds. .PP .Vb 2 \& # Increase inactivity timeout for connection to 300 seconds \& Mojo::IOLoop\->stream($c\->tx\->connection)\->timeout(300); .Ve .ie n .SS """session""" .el .SS "\f(CWsession\fP" .IX Subsection "session" .Vb 4 \& my $session = $c\->session; \& my $foo = $c\->session(\*(Aqfoo\*(Aq); \& $c = $c\->session({foo => \*(Aqbar\*(Aq}); \& $c = $c\->session(foo => \*(Aqbar\*(Aq); .Ve .PP Persistent data storage, stored \f(CW\*(C`JSON\*(C'\fR serialized in a signed cookie. Note that cookies are generally limited to 4096 bytes of data. .PP .Vb 4 \& # Manipulate session \& $c\->session\->{foo} = \*(Aqbar\*(Aq; \& my $foo = $c\->session\->{foo}; \& delete $c\->session\->{foo}; \& \& # Delete whole session \& $c\->session(expires => 1); .Ve .ie n .SS """signed_cookie""" .el .SS "\f(CWsigned_cookie\fP" .IX Subsection "signed_cookie" .Vb 4 \& my $value = $c\->signed_cookie(\*(Aqfoo\*(Aq); \& my @values = $c\->signed_cookie(\*(Aqfoo\*(Aq); \& $c = $c\->signed_cookie(foo => \*(Aqbar\*(Aq); \& $c = $c\->signed_cookie(foo => \*(Aqbar\*(Aq, {path => \*(Aq/\*(Aq}); .Ve .PP Access signed request cookie values and create new signed response cookies. Cookies failing signature verification will be automatically discarded. .ie n .SS """stash""" .el .SS "\f(CWstash\fP" .IX Subsection "stash" .Vb 4 \& my $stash = $c\->stash; \& my $foo = $c\->stash(\*(Aqfoo\*(Aq); \& $c = $c\->stash({foo => \*(Aqbar\*(Aq}); \& $c = $c\->stash(foo => \*(Aqbar\*(Aq); .Ve .PP Non persistent data storage and exchange, application wide default values can be set with \*(L"defaults\*(R" in Mojolicious. Many stash values have a special meaning and are reserved, the full list is currently \f(CW\*(C`action\*(C'\fR, \f(CW\*(C`app\*(C'\fR, \f(CW\*(C`cb\*(C'\fR, \&\f(CW\*(C`controller\*(C'\fR, \f(CW\*(C`data\*(C'\fR, \f(CW\*(C`extends\*(C'\fR, \f(CW\*(C`format\*(C'\fR, \f(CW\*(C`handler\*(C'\fR, \f(CW\*(C`json\*(C'\fR, \f(CW\*(C`layout\*(C'\fR, \&\f(CW\*(C`namespace\*(C'\fR, \f(CW\*(C`partial\*(C'\fR, \f(CW\*(C`path\*(C'\fR, \f(CW\*(C`status\*(C'\fR, \f(CW\*(C`template\*(C'\fR and \f(CW\*(C`text\*(C'\fR. .PP .Vb 4 \& # Manipulate stash \& $c\->stash\->{foo} = \*(Aqbar\*(Aq; \& my $foo = $c\->stash\->{foo}; \& delete $c\->stash\->{foo}; .Ve .ie n .SS """ua""" .el .SS "\f(CWua\fP" .IX Subsection "ua" .Vb 1 \& my $ua = $c\->ua; .Ve .PP Alias for \*(L"ua\*(R" in Mojo. .PP .Vb 3 \& # Blocking \& my $tx = $c\->ua\->get(\*(Aqhttp://mojolicio.us\*(Aq); \& my $tx = $c\->ua\->post_form(\*(Aqhttp://kraih.com/login\*(Aq => {user => \*(Aqmojo\*(Aq}); \& \& # Non\-blocking \& $c\->ua\->get(\*(Aqhttp://mojolicio.us\*(Aq => sub { \& my ($ua, $tx) = @_; \& $c\->render_data($tx\->res\->body); \& }); \& \& # Parallel non\-blocking \& my $delay = Mojo::IOLoop\->delay(sub { \& my ($delay, @titles) = @_; \& $c\->render_json(\e@titles); \& }); \& for my $url (\*(Aqhttp://mojolicio.us\*(Aq, \*(Aqhttps://metacpan.org\*(Aq) { \& $delay\->begin; \& $c\->ua\->get($url => sub { \& my ($ua, $tx) = @_; \& $delay\->end($tx\->res\->dom\->html\->head\->title\->text); \& }); \& } .Ve .ie n .SS """url_for""" .el .SS "\f(CWurl_for\fP" .IX Subsection "url_for" .Vb 5 \& my $url = $c\->url_for; \& my $url = $c\->url_for(name => \*(Aqsebastian\*(Aq); \& my $url = $c\->url_for(\*(Aqtest\*(Aq, name => \*(Aqsebastian\*(Aq); \& my $url = $c\->url_for(\*(Aq/perldoc\*(Aq); \& my $url = $c\->url_for(\*(Aqhttp://mojolicio.us/perldoc\*(Aq); .Ve .PP Generate a portable Mojo::URL object with base for a route, path or \s-1URL\s0. .PP .Vb 2 \& # "/perldoc?foo=bar" if application is deployed under "/" \& $c\->url_for(\*(Aq/perldoc\*(Aq)\->query(foo => \*(Aqbar\*(Aq); \& \& # "/myapp/perldoc?foo=bar" if application is deployed under "/myapp" \& $c\->url_for(\*(Aq/perldoc\*(Aq)\->query(foo => \*(Aqbar\*(Aq); .Ve .PP You can also use the helper \*(L"url_with\*(R" in Mojolicious::Plugin::DefaultHelpers to inherit query parameters from the current request. .PP .Vb 2 \& # "/list?q=mojo&page=2" if current request was for "/list?q=mojo&page=1" \& $c\->url_with\->query([page => 2]); .Ve .ie n .SS """write""" .el .SS "\f(CWwrite\fP" .IX Subsection "write" .Vb 4 \& $c\->write; \& $c\->write(\*(AqHello!\*(Aq); \& $c\->write(sub {...}); \& $c\->write(\*(AqHello!\*(Aq, sub {...}); .Ve .PP Write dynamic content non-blocking, the optional drain callback will be invoked once all data has been written. .PP .Vb 6 \& # Keep connection alive (with Content\-Length header) \& $c\->res\->headers\->content_length(6); \& $c\->write(\*(AqHel\*(Aq, sub { \& my $c = shift; \& $c\->write(\*(Aqlo!\*(Aq) \& }); \& \& # Close connection when finished (without Content\-Length header) \& $c\->write(\*(AqHel\*(Aq, sub { \& my $c = shift; \& $c\->write(\*(Aqlo!\*(Aq, sub { \& my $c = shift; \& $c\->finish; \& }); \& }); .Ve .PP For Comet (\f(CW\*(C`long polling\*(C'\fR) you might also want to increase the inactivity timeout, which usually defaults to \f(CW15\fR seconds. .PP .Vb 2 \& # Increase inactivity timeout for connection to 300 seconds \& Mojo::IOLoop\->stream($c\->tx\->connection)\->timeout(300); .Ve .ie n .SS """write_chunk""" .el .SS "\f(CWwrite_chunk\fP" .IX Subsection "write_chunk" .Vb 4 \& $c\->write_chunk; \& $c\->write_chunk(\*(AqHello!\*(Aq); \& $c\->write_chunk(sub {...}); \& $c\->write_chunk(\*(AqHello!\*(Aq, sub {...}); .Ve .PP Write dynamic content non-blocking with \f(CW\*(C`chunked\*(C'\fR transfer encoding, the optional drain callback will be invoked once all data has been written. .PP .Vb 8 \& # Make sure previous chunk has been written before continuing \& $c\->write_chunk(\*(AqHe\*(Aq, sub { \& my $c = shift; \& $c\->write_chunk(\*(Aqll\*(Aq, sub { \& my $c = shift; \& $c\->finish(\*(Aqo!\*(Aq); \& }); \& }); .Ve .PP You can call \f(CW\*(C`finish\*(C'\fR at any time to end the stream. .PP .Vb 7 \& 2 \& He \& 2 \& ll \& 2 \& o! \& 0 .Ve .SH "HELPERS" .IX Header "HELPERS" In addition to the attributes and methods above you can also call helpers on Mojolicious::Controller objects. This includes all helpers from Mojolicious::Plugin::DefaultHelpers and Mojolicious::Plugin::TagHelpers. .PP .Vb 2 \& $c\->layout(\*(Aqgreen\*(Aq); \& $c\->title(\*(AqWelcome!\*(Aq); .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" Mojolicious, Mojolicious::Guides, .