[Catalyst] Creating my own controller and another question

Matt S Trout dbix-class at trout.me.uk
Fri Apr 28 18:45:40 CEST 2006


John Napiorkowski wrote:
> Thanks for such a quick reply.  I'm still shaking my
> head about the strange config problem.  I think I'll
> have to break out a separate instance of catalyst to
> clarify it and try to write a test case for it.

The other reply explains the problem. Making the code do the _config({ %{ 
_config } }) trick I described in my last mail should fix it.

> One thing that I am rather confused about is how and
> when $self and $c appear or not.  I got that you have
> to use the "my ($self, $c) = @_);" and not try to
> shift them in the way I am used to with other perl
> development.  However I get into a lot of trouble when
> I need the $c but only have $self (such as when I have
> some controller accessors) or I have $c but not $self
> (such as is the default with template toolkit
> templates).  I am not sure what you mean when you say:

The first is fixed by passing $c, using ACCEPT_CONTEXT or making the method an 
action and ->forward-ing to it.

The second is simply not true, [% c.controller %] will give the primary 
controller for a request on latest Catalyst, and [% c.comp(c.action.class) %] 
will work right back to 5.50.

>> when Catalyst retrieves a component of any type for
>> use, if the component has 
>> an ACCEPT_CONTEXT method it calls that with $c as an
>> argument and returns the 
>> result.

"when Catalyst retrieves a component" as in "when $c->comp or 
->model/view/controller is called". Which Catalyst does to provide the 
execution environment for actions.

> For example, in the code snippet you suggested I could
> used to override the new subroutine for a subclass of
> controller, would there be any way to get the $c
> context object?
> 
>> sub new {
>>    my $self = shift;
>>    my $new = $self->NEXT::new(@_);
>>    $new->init;
>>    return $new;
>> }
> 
> From the source code of Catalyst::Component it looks
> like I could just say "my ( $self, $c ) = @_;" instead
> of "my $self = shift;" but for some reason I have
> trouble with that.  Should that work?  And how could I
> get the context object if I had something like:

Read through the source to Catalyst::Component again, and bear in mind you can 
always do things like

my $self = shift;
my ($c) = @_;

> package mycontroller;
> use base 'Catalyst::Controller';
> 
> sub index: Private
> {
>      my ($self, $c) = @_;
> 
>     $self->test;
> }
> 
> sub test
> {
>      my $self = shift @_;
> 
>      ##I NEED the $c here?
> }

if you need it, either stash it at ACCEPT_CONTEXT time, pass it through 
explicitly, or make test :Private and $c->forward('test');

> Maybe there is different best practices way for me to
> do this.  Your suggestions would be helpful.

It depends what you're trying to achieve, and the contrived examples you've 
posted so far don't make that remotely clear.

> On a side issue, right now I am working on a project
> using catalyst and trying to get my client to agree to
> me posting the code in a public location for other
> developers to check out.  If I can do this is there a
> place where catalyst developers prefer to find such
> examples?  For example I noticed a list of catalyst
> driven sites on the developers wiki.

Depending on the license it might be possible to accept the code to examples/ 
in trunk. Otherwise, linking it from the wiki is probably good.

-- 
      Matt S Trout       Offering custom development, consultancy and support
   Technical Director    contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +



More information about the Catalyst mailing list