Sometimes you need to access $c before Catalyst processes the request to set data in context. A convenient place to do this is after prepare runs.

You can't use

after 'prepare' => sub my $c = shift; ... }

because prepare takes the class and creates the $c. Instead, use MooseX::Mangle:

use MooseX::Mangle;
mangle_return 'prepare' => sub {
  my($class$c) = @_;
  # do whatever with $c
  return $c;
}

Here is a complete example:

use warnings;
use strict;

package Catalyst::Plugin::Log::Log4perl::MDC;
our $VERSION = '1.00';

use namespace::autoclean;
use Moose::Role;
use MooseX::Mangle;

after 'setup_finalize' => sub {
  Catalyst::Utils::ensure_class_loaded('Log::Log4perl::MDC');
};

mangle_return 'prepare' => sub {
  my ($class$c) = @_;
  my $session = $c->session# force app to load session
  Log::Log4perl::MDC->put('sessionid'$c->sessionid);
  $c;
};

1;
My tags:
 
Popular tags:
 
Powered by Catalyst
Powered by MojoMojo Hosted by Shadowcat - Managed by Nordaaker