1Mojolicious::ControllerU(s3e)r Contributed Perl DocumentaMtoijoonlicious::Controller(3)
2
3
4
6 Mojolicious::Controller - Controller Base Class
7
9 use base 'Mojolicious::Controller';
10
12 Mojolicous::Controller is the base class for your Mojolicious
13 controllers. It is also the default controller class for Mojolicious
14 unless you set "controller_class" in your application.
15
17 Mojolicious::Controller inherits all attributes from
18 MojoX::Dispatcher::Routes::Controller.
19
21 Mojolicious::Controller inherits all methods from
22 MojoX::Dispatcher::Routes::Controller and implements the following new
23 ones.
24
25 "client"
26 my $client = $c->client;
27
28 A Mojo::Client prepared for the current environment.
29
30 my $tx = $c->client->get('http://mojolicious.org');
31
32 $c->client->post_form('http://kraih.com/login' => {user => 'mojo'});
33
34 $c->client->get('http://mojolicious.org' => sub {
35 my $client = shift;
36 $c->render_data($client->res->body);
37 })->process;
38
39 For async processing you can use "pause" and "finish".
40
41 $c->pause;
42 $c->client->async->get('http://mojolicious.org' => sub {
43 my $client = shift;
44 $c->render_data($client->res->body);
45 $c->finish;
46 })->process;
47
48 "finish"
49 $c->finish;
50
51 Similar to "resume" but will also trigger automatic rendering and the
52 "after_dispatch" plugin hook, which would normally get disabled once a
53 request gets paused. For WebSockets it will gracefully end the
54 connection.
55
56 "finished"
57 $c->finished(sub {...});
58
59 Callback signaling that the transaction has been finished.
60
61 $c->finished(sub {
62 my $self = shift;
63 });
64
65 "helper"
66 $c->helper('foo');
67 $c->helper(foo => 23);
68
69 Directly call a Mojolicious helper, see
70 Mojolicious::Plugin::DefaultHelpers for a list of helpers that are
71 always available.
72
73 "pause"
74 $c->pause;
75
76 Pause transaction associated with this request, used for async web
77 applications. Note that automatic rendering and some plugins that do
78 state changing operations inside the "after_dispatch" hook won't work
79 if you pause a transaction.
80
81 "receive_message"
82 $c = $c->receive_message(sub {...});
83
84 Receive messages via WebSocket, only works if there is currently a
85 WebSocket connection in progress.
86
87 $c->receive_message(sub {
88 my ($self, $message) = @_;
89 });
90
91 "redirect_to"
92 $c = $c->redirect_to('named');
93 $c = $c->redirect_to('named', foo => 'bar');
94 $c = $c->redirect_to('/path');
95 $c = $c->redirect_to('http://127.0.0.1/foo/bar');
96
97 Prepare a redirect response.
98
99 "render"
100 $c->render;
101 $c->render(controller => 'foo', action => 'bar');
102 $c->render({controller => 'foo', action => 'bar'});
103 $c->render(text => 'Hello!');
104 $c->render(template => 'index');
105 $c->render(template => 'foo/index');
106 $c->render(template => 'index', format => 'html', handler => 'epl');
107 $c->render(handler => 'something');
108 $c->render('foo/bar');
109 $c->render('foo/bar', format => 'html');
110 $c->render('foo/bar', {format => 'html'});
111
112 This is a wrapper around MojoX::Renderer exposing pretty much all
113 functionality provided by it. It will set a default template to use
114 based on the controller and action name or fall back to the route name.
115 You can call it with a hash of options which can be preceded by an
116 optional template name. Note that all render arguments get localized,
117 so stash values won't be changed after the render call.
118
119 "render_data"
120 $c->render_data($bits);
121
122 Render binary data, similar to "render_text" but data will not be
123 encoded.
124
125 "render_exception"
126 $c->render_exception($e);
127
128 Render the exception template "exception.html.$handler". Will set the
129 status code to 500 meaning "Internal Server Error". Takes a
130 Mojo::Exception object or error message and will fall back to rendering
131 a static 500 page using MojoX::Renderer::Static.
132
133 "render_inner"
134 my $output = $c->render_inner;
135 my $output = $c->render_inner('content');
136 my $output = $c->render_inner(content => 'Hello world!');
137 my $output = $c->render_inner(content => sub { 'Hello world!' });
138
139 Contains partial rendered templates, used for the renderers "layout"
140 and "extends" features.
141
142 "render_json"
143 $c->render_json({foo => 'bar'});
144 $c->render_json([1, 2, -3]);
145
146 Render a data structure as JSON.
147
148 "render_not_found"
149 $c->render_not_found;
150 $c->render_not_found($resource);
151
152 Render the not found template "not_found.html.$handler". Also sets the
153 response status code to 404, will fall back to rendering a static 404
154 page using MojoX::Renderer::Static.
155
156 "render_partial"
157 my $output = $c->render_partial;
158 my $output = $c->render_partial(action => 'foo');
159
160 Same as "render" but returns the rendered result.
161
162 "render_static"
163 $c->render_static('images/logo.png');
164
165 Render a static asset using MojoX::Dispatcher::Static.
166
167 "render_text"
168 $c->render_text('Hello World!');
169 $c->render_text('Hello World', layout => 'green');
170
171 Render the given content as plain text, note that text will be encoded.
172 See "render_data" for an alternative without encoding.
173
174 "resume"
175 $c->resume;
176
177 Resume transaction associated with this request, used for async web
178 applications.
179
180 "send_message"
181 $c = $c->send_message('Hi there!');
182
183 Send a message via WebSocket, only works if there is currently a
184 WebSocket connection in progress.
185
186 "url_for"
187 my $url = $c->url_for;
188 my $url = $c->url_for(controller => 'bar', action => 'baz');
189 my $url = $c->url_for('named', controller => 'bar', action => 'baz');
190
191 Generate a Mojo::URL for the current or a named route.
192
194 Mojolicious, Mojolicious::Guides, <http://mojolicious.org>.
195
196
197
198perl v5.12.3 2010-08-17 Mojolicious::Controller(3)