4 package Catalyst::Middleware::Stash;
6 use base 'Plack::Middleware';
10 our @EXPORT_OK = qw(stash get_stash);
12 sub PSGI_KEY () { 'Catalyst.Stash.v1' }
16 return $env->{+PSGI_KEY} ||
17 croak "You requested a stash, but one does not exist.";
21 my ($host, @args) = @_;
22 return get_stash($host->env)
27 my $stash = shift || +{};
30 my $new_stash = @_ > 1 ? {@_} : $_[0];
31 croak('stash takes a hash or hashref')
32 unless ref $new_stash;
33 foreach my $key (keys %$new_stash) {
34 $stash->{$key} = $new_stash->{$key};
42 my ($self, $env) = @_;
43 my $new_env = +{ %$env };
44 my %stash = %{ ($env->{+PSGI_KEY} || sub {})->() || +{} };
46 $new_env->{+PSGI_KEY} = _create_stash( \%stash );
47 return $self->app->($new_env);
52 Catalyst::Middleware::Stash - The Catalyst stash - in middleware
56 We've moved the L<Catalyst> stash to middleware. Please don't use this
57 directly since it is likely to move off the Catalyst namespace into a stand
60 We store a coderef under the C<PSGI_KEY> which can be dereferenced with
61 key values or nothing to access the underlying hashref.
63 The stash middleware is designed so that you can 'nest' applications that
64 use it. If for example you have a L<Catalyst> application that is called
65 by a controller under a parent L<Catalyst> application, the child application
66 will inherit the full stash of the parent BUT any new keys added by the child
67 will NOT bubble back up to the parent. However, children of children will.
69 For more information the current test case t/middleware-stash.t is the best
74 This class defines the following subroutines.
78 Returns the hash key where we store the stash. You should not assume
79 the string value here will never change! Also, its better to use
80 L</get_stash> or L</stash>.
86 Exportable subroutine.
88 Get the stash out of the C<$env>.
92 Expects: An object that does C<env> and arguments
94 Exportable subroutine.
96 Given an object with a method C<env> get or set stash values, either
97 as a method or via hashref modification. This stash is automatically
98 reset for each request (it is not persistent or shared across connected
99 clients. Stash key / value are stored in memory.
102 use Catalyst::Middleware::Stash 'stash';
106 my $req = Plack::Request->new($env);
107 my $stashed = $req->stash->{in_the_stash}; # Assume the stash was previously populated.
109 return [200, ['Content-Type' => 'text/plain'],
110 ["I found $stashed in the stash!"]];
113 If the stash does not yet exist, an exception is thrown.
117 This class defines the following methods.
121 Used by plack to call the middleware