Skip to content

Commit d34738a

Browse files
committed
Documentation improvements
1 parent ceecc66 commit d34738a

File tree

4 files changed

+32
-65
lines changed

4 files changed

+32
-65
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Revision history for Kelp
33
{{$NEXT}}
44
[Changes]
55
- Fixed psgi routes not working with custom controllers
6+
- Documentation improvements
67

78
2.16 - 2024-07-05
89
[New Interface]

lib/Kelp/Context.pm

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,20 @@ Returns a controller of a given name. The name will be mangled according to the
131131
base route class of the application. Contains extra checks to ensure the input
132132
is valid and loads the controller class if it wasn't loaded yet.
133133
134-
If the controller name is undef, the base controller is returned.
134+
If the controller name is C<undef>, the base controller is returned.
135135
136136
=head2 set_controller
137137
138-
Like L</controller>, but does not have any special checks for correctness and
139-
only accepts a full class name. It also modifies the L</current> to the
140-
controller after constructing it. It's optimized for speed and used only
141-
internally.
138+
Similar to L</controller>, but does not have any special checks for correctness
139+
and only accepts a full class name. It also modifies the L</current> to the
140+
controller after constructing it. Passing a false value will result in
141+
reverting the current context back to the app object.
142+
143+
It's optimized for speed and only used internally, so it's not recommended to
144+
use it unless you extend Kelp router itself.
142145
143146
=head2 clear
144147
145-
Clears context in anticipation of the next request.
148+
Clears context in anticipation of the next request. Called automatically at the
149+
start of every request.
146150

lib/Kelp/Manual/Controllers.pod

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ class.
1616

1717
Controllers lets you separate some of the route handling logic to other classes
1818
and have your subs take the object of the correct class as the first argument.
19-
In Kelp, there is no special base class for controllers - all controllers must
20-
be subclasses of L<Kelp>.
19+
In Kelp, there is no special base class for controllers by default - all
20+
controllers must be subclasses of L<Kelp>, unless you want to develop your own
21+
base controller - see L</Use different method than reblessing>.
2122

2223
=head2 Reblessing details
2324

2425
Reblessing will happen after request is matched to a route. Route handler has
2526
to be specified as class and method string, and class must be a subclass of class
26-
configured for L<Kelp::Routes/base>. L<Kelp::Routes/rebless> must albo be
27+
configured for L<Kelp::Routes/base>. L<Kelp::Routes/rebless> must also be
2728
enabled for that to occur.
2829

2930
The default value of C<base> field is the application class, so your
@@ -206,8 +207,8 @@ default C<context_obj>, which is the class name of the context:
206207

207208
=head1 CAVEATS
208209

209-
There are some controller gotchas which come from a fact that they are not
210-
constructed like a regular object:
210+
There are some controller gotchas which come from a fact that they are not by
211+
default constructed like a regular object:
211212

212213
=head2 Main application object is shallow-cloned before rebless
213214

@@ -233,12 +234,12 @@ long as you don't instantiate your controllers before app building is finished.
233234

234235
Controllers are never actually constructed, but instead the main app object is
235236
cloned and reblessed into the correct class. Don't expect your override of
236-
C<new> or C<build> to work.
237+
C<new> or C<build> to ever be called. No automatic controller initialization
238+
happens in Kelp.
237239

238-
No automatic controller initialization happens in Kelp. If you'd like to access
239-
a controller in other context than route handling - for example in C<build>
240-
method, allowing you to move route definitions to the controller - you may use
241-
C<context> tracking object:
240+
If you'd like to access a controller in other context than route handling - for
241+
example in C<build> method, allowing you to move route definitions to the
242+
controller - you may use C<context> tracking object:
242243

243244
# in MyApp.pm
244245
sub build {

lib/Kelp/Manual/Cookbook.pod

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,9 @@ A slightly shorter version with state variables and no ping:
8989

9090
# Use $self->dbh from here on ...
9191

92-
sub some_route {
93-
my $self = shift;
94-
95-
$self->dbh->selectrow_array(q[
96-
SELECT * FROM users
97-
WHERE clue > 0
98-
]);
99-
}
100-
10192
Same methods can be used for accessing the schema of L<DBIx::Class>.
10293

103-
=head2 Custom 404 and 500 error pages
94+
=head2 Custom error pages
10495

10596
=head3 Error templates
10697

@@ -112,49 +103,14 @@ C<< $self->res->render_error >>.
112103

113104
=head3 Within the route
114105

115-
You can set the response headers and content within the route:
106+
For one-off rendering of errors, you can alternatively set the response headers
107+
and content within the route:
116108

117109
sub some_route {
118110
my $self = shift;
119111
$self->res->set_code(404)->template('my_404_template');
120112
}
121113

122-
=head3 By overriding the Kelp::Response class
123-
124-
To make custom 500, 404 and other error pages, you will have to subclass the
125-
L<Kelp::Response> module and override the I<render_404> and I<render_500> subroutines.
126-
Let's say your app's name is Foo and its class is in I<lib/Foo.pm>. Now create a
127-
file I<lib/Foo/Response.pm>:
128-
129-
package Foo::Response;
130-
use Kelp::Base 'Kelp::Response';
131-
132-
sub render_404 {
133-
my $self = shift;
134-
$self->template('my_custom_404');
135-
}
136-
137-
sub render_500 {
138-
my $self = shift;
139-
$self->template('my_custom_500');
140-
}
141-
142-
Then, in I<lib/Foo.pm>, you have to tell Kelp to use your custom response class
143-
like this:
144-
145-
sub response {
146-
my $self = shift;
147-
return Foo::Response->new( app => $self );
148-
}
149-
150-
Don't forget you need to create I<views/my_custom_404.tt> and
151-
I<views/my_custom_500.tt>. You can add other error rendering subroutines too, for
152-
example:
153-
154-
sub render_401 {
155-
# Render your custom 401 error here
156-
}
157-
158114
=head2 Altering the behavior of a Kelp class method
159115

160116
The easiest solution would be to use L<KelpX::Hooks> module available on CPAN:
@@ -228,9 +184,14 @@ use L<Plack::Middleware::ReverseProxy>.
228184

229185
(REMOTE_ADDR is not set at all when using the proxy via filesocket).
230186

231-
=head2 Changing the default access logging
187+
=head2 Changing the default logging
188+
189+
Default log format can be modified by configuring the C<Logger> module. See
190+
L<Kelp::Module::Logger/date_format> and L<Kelp::Module::Logger/log_format>.
191+
Alternatively, L<Log::Dispatch> can be configured with its own callback to
192+
format the message to be logged.
232193

233-
Access logs reported by Kelp through C<logger> can be modified or disabled by
194+
Access logs reported by Kelp through C<Logger> can be modified or disabled by
234195
writing your own customized L<Kelp/before_dispatch> method (not calling the
235196
parent version).
236197

0 commit comments

Comments
 (0)