List Info

Thread: access stash outside methods




access stash outside methods
user name
2007-08-21 09:31:40
Hi,

I am running catalyst under apache configured with mod_perl.

Can I somehow access stash out methods.

I want to declare a global variable inside controller and outside methods which will be accessible in all controllers.
stash could be a solution but outside methods context object is not available so i don't have access to stash.

please help me solve the problem.

Thanks,
Aditya
Re: access stash outside methods
country flaguser name
United States
2007-08-21 10:39:02
--- Aditya Verma <verma.aditya1gmail.com> wrote:

> Hi,
> 
> I am running catalyst under apache configured with
> mod_perl.
> 
> Can I somehow access stash out methods.
> 
> I want to declare a global variable inside
> controller and outside methods
> which will be accessible in all controllers.
> stash could be a solution but outside methods
> context object is not
> available so i don't have access to stash.
> 
> please help me solve the problem.
> 
> Thanks,
> Aditya

Hi,

I may not fully understand your use case, but
typically the stash is used for a particular context,
not as a global thing.

You can alway pass the context to methods that need
it.  I know it seems cumbersome, but the reason is to
make you really think if you need it, the idea being
that you should try to decouple your context as much
as possible.

If you need some global information there are a few
ways I can think of:

1) put it into the configuration file.  This is great
for static stuff, like admin email address, etc.
2) Create a model that provides the information you
need.  If this information is based on the current
context you can inherit from
"Catalyst::Component::ACCEPT_CONTEXT" to build
it.
3) Create a base controller with a method that gets
the information you need.  The have the controllers
that need this information inherit from that.

You might get the suggestion to make this a catalyst
plugin, but that's no longer recommended best
practices.  Plugins should be reserved for when you
are trulying enhancing/extending the basic Catalyst
features.

Please share with us the details of the use case and
maybe we can help.

--John 


       
____________________________________________________________
________________________
Choose the right car based on your needs.  Check out Yahoo!
Autos new Car Finder tool.
http://autos.yahoo.
com/carfinder/

_______________________________________________
List: Catalystlists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalystlists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/

Re: access stash outside methods
country flaguser name
United States
2007-08-22 09:43:43
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
emailjasonkohles.com
http://www.jasonkohles.co
m/
"A witty saying proves nothing."  -- Voltaire



_______________________________________________
List: Catalystlists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalystlists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )