NAME

Thunderhorse::Cookbook - Reusable coding patterns

RECIPES

Custom error pages

Making prettier app-wide error pages is easy by overriding "render_error" in Thunderhorse::App:

async sub render_error ($self, $controller, $ctx, $code, $message = undef)
{
	$controller //= $self->controller;
	my $html;

	try {
		$html = $controller->template("error/$code");
	}
	catch ($ex) {
		$html = $controller->template('error/error', {title => status_message($code)});
	}

	await $ctx->res->status($code)->html($html);
}

For example for error 404, code above will try to render error/404.tt template. If it is not found, it will render more generic error/error.tt instead.

Same can be done on controller level, which means different parts of the system can use different error pages without any issue.

Reusing common bridge across controllers

If you need to make a common bridge that will be used across the system, it is best to define it in the base application class and give it a name:

# in MyApp.pm
sub build ($self)
{
	$self->router->add(
		'/admin' => {
			name => 'admin_auth',
			to => 'check_auth',
		}
	);

	# make sure the controller is loaded after the bridge is added
	# (done automatically if loading from configuration)
	$self->load_controller('ReusesBridge');
}

Then you can fetch it in one of the controllers, which allows adding more children locations:

# in MyApp/Controller/ReusesBridge.pm
sub build ($self)
{
	my $bridge = $self->router->find('admin_auth')
	$bridge->add(...);
}

SEE ALSO

Thunderhorse::Manual - table of contents