[Catalyst] access stash outside methods
Jason Kohles
email at jasonkohles.com
Wed Aug 22 15:43:43 GMT 2007
On Aug 21, 2007, at 3:48 PM, Aditya Verma wrote:
> Hi All,
>
> I want all of my controllers to register there respective URLs with
> description. this data will be used in template to create dynamic
> HTML pages.
> controllers will register URLs at catalyst startup.
>
> I have to store this data into variable (not in any file or
> database) which will be accessible in all the controllers.
> To get the registration work I have to write statement outside of
> all controller methods to execute the same at the time of catalyst
> startup.
>
> hope you guys can understand my requirement.
>
Data that only gets registered at startup like this doesn't belong in
the stash anyway, I usually do this with class data...
package MyApp::Controller;
use base qw( Catalyst::Controller );
__PACKAGE__->mk_classdata( 'url_descriptions' => {} );
sub describe_url {
my ( $self, $url, $description ) = @_;
my $class = ref( $self ) || $self;
$class->url_descriptions->{ $url } = $description;
}
package MyApp::Controller::Root;
use base qw( MyApp::Controller );
__PACKAGE__->describe_url( '/' => 'The Main Page' );
package MyApp::Controller::PageListing;
use base qw( MyApp::Controller );
use Class::Inspector;
sub assemble_url_descriptions {
my ( $self ) = @_;
my %descriptions = ();
for my $class ( Class::Inspector->subclasses( 'MyApp::Controller' ) ) {
while ( my ( $url, $desc ) = each %{ $class->url_descriptions } ) {
$descriptions{ $url } = $desc;
}
}
return \%descriptions;
}
--
Jason Kohles
email at jasonkohles.com
http://www.jasonkohles.com/
"A witty saying proves nothing." -- Voltaire
More information about the Catalyst
mailing list