FAQ
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20070821/dc813c25/attachment.htm

Search Discussions

  • John Napiorkowski at Aug 21, 2007 at 4:38 pm

    --- Aditya Verma 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/
  • Aditya Verma at Aug 21, 2007 at 8:48 pm
    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.

    Thanks,
    Aditya
    On 8/21/07, John Napiorkowski wrote:


    --- Aditya Verma 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: Catalyst@lists.rawmode.org
    Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
    Searchable archive:
    http://www.mail-archive.com/catalyst@lists.rawmode.org/
    Dev site: http://dev.catalyst.perl.org/
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20070822/54c6914f/attachment.htm
  • John Napiorkowski at Aug 22, 2007 at 12:12 am

    --- 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.

    Thanks,
    Aditya
    Aditya,

    Since your actions can map URL's in different ways, it
    may not be easy to dynamically create a sitemap object
    in the way you are talking about. Because if an
    action accepts parameters than it can potentially map
    to a large number of URLs.

    If you just want to have some additional information
    placed into the stash for all the actions in a given
    controller, you can do that in a :begin action, which
    is run for each request to a controller.

    However if this information is presentation related
    you might prefer to place it into a configuration file
    for your view templates. For example if you are using
    Template Toolkit, you can include a template that just
    defines some local information. That way you can
    modify the information without having to deal with the
    controllers.

    Not sure if I am helping. Good luck!
    John

    On 8/21/07, John Napiorkowski wrote:


    --- Aditya Verma 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: Catalyst@lists.rawmode.org
    Listinfo:
    http://lists.rawmode.org/mailman/listinfo/catalyst
    Searchable archive:
    http://www.mail-archive.com/catalyst@lists.rawmode.org/
    Dev site: http://dev.catalyst.perl.org/

    _______________________________________________
    List: Catalyst@lists.rawmode.org
    Listinfo:
    http://lists.rawmode.org/mailman/listinfo/catalyst
    Searchable archive:
    http://www.mail-archive.com/catalyst@lists.rawmode.org/
    Dev site: http://dev.catalyst.perl.org/



    ____________________________________________________________________________________
    Be a better Heartthrob. Get better relationship answers from someone who knows. Yahoo! Answers - Check it out.
    http://answers.yahoo.com/dir/?link=list&sid96545433
  • Jonathan Rockway at Aug 22, 2007 at 1:59 am

    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.
    How about using attributes?

    pacakge MyApp::Controller::Anything;
    use base 'MyApp::Controler::HasDescription';

    sub some_action
    :Path :Args(1) :URL('/some_action/*') :Description('Some
    action!') {
    # your action
    }
    # etc.

    Those get parsed at startup time, and you can probably shove them in
    $app->config or somewhere similarly convenient. You can also just walk
    all actions and look for the URL and Description attributes. This is
    pretty easy to do, but I don't know the details so I will let you glean
    them from the code/docs. $c->action_for('/some_action')->attributes
    should be close. Have fun.

    Regards,
    Jonathan Rockway




    -------------- next part --------------
    A non-text attachment was scrubbed...
    Name: signature.asc
    Type: application/pgp-signature
    Size: 370 bytes
    Desc: OpenPGP digital signature
    Url : http://lists.scsys.co.uk/pipermail/catalyst/attachments/20070821/c082c2d8/signature.pgp
  • Jason Kohles at Aug 22, 2007 at 3:41 pm

    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@jasonkohles.com
    http://www.jasonkohles.com/
    "A witty saying proves nothing." -- Voltaire
  • Matt Rosin at Aug 21, 2007 at 6:28 pm
    Your use of terms is a little confusing, can you explain better what
    you want to do? What do you mean by "outside methods context object"?

    You don't really want to declare a perl global variable since this
    could hurt your persistent server. Perhaps you want (Model) class data
    or you want to put some data in $c->config with myapp.yml as John
    suggests.

    You mentioned a context object, this can have different meanings. You
    can persist data through Catalyst::Plugin::Session (add to session or
    use $c->flash) which could simulate a persistent context within a user
    session (like a persisted stash). Or perhaps store state information
    in a user object in the database which might be more robust since it
    doesn't depend on session files or cookies.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedAug 21, '07 at 3:31p
activeAug 22, '07 at 3:41p
posts7
users5
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2022 Grokbase