How to use Catalyst auto and get started?

I need two special methods:

  • which runs for all urls
  • which only runs for a specific path (/ admin)

I thought the most common would use begin

, and the method for / admin would use auto

. For example, in these two Catalyst controllers:

package MyApp::Controller::Root;

sub begin :Private {
    my ($self, $c) = @_;

    $c->log->debug('Run for all URLs');
}

      

[...]

package MyApp::Controller::Admin;

sub auto :Private {
    my ($self, $c) = @_;

    $c->log->debug('Run for /admin only');
}

      

But it doesn't achieve what I want. What's the correct decision?

EDIT : The problem is that it Addmin::auto()

never gets called, not when I access / admin or / admin /

auto

Never called after additional tests . I tried to put auto in different places, it never gets called.

+2


source to share


3 answers


The problem was this, both controllers had the following line:

__PACKAGE__->config->{namespace} = '';

      



This prevented the automatic function from running in Admin.pm

+3


source


There is no obvious reason why what you have described does not do what you want it to do. This would be the correct way to do it.

The log should indicate the sending path and whether your request was redirected through these actions or not. If not, it will tell you how it is processed.



The second line of each subsector must be terminated with ';'. I am assuming there is a typo in SO and not your original code.

+3


source


Do you have a begin action in Controller :: Admin? As RET says, the way you described things should work fine; the only caveat with the "global start" is that if you put the start in any other controller, it is "shadow" global, because only one action is triggered per action, and that is the "most specific" (longest in namespace terms private path).

+1


source







All Articles