Search code examples
perlcatalyst

How should I disable an action via a config file in Catalyst?


I have an controller that has actions that are set up using chained. My chained root action is in my root controller, then my 'section' controller has a 'root' action chained from the root controller's 'root' action. I then have endpoint actions in the 'section' controller class that chain from the 'root' action in the same class:

 package MyApp::Controller::Root;

 sub root :Chained('/') PathPart('') CaptureArgs(0) {}

 package MyApp::Controller::Section;

 sub root :Chained('/root') PathPrefix CaptureArgs(0) {}

 sub foo :Chained('root') PathPart Args(0) {}

How can I disable the all the actions in the 'section' package via a config file? What I have done so far is make the 'root' action in the section class Private and that seems to work, but how can I tell that the section is not available when I build by navigation? I can try uri_for_action and that returns undef, but that seems a bit messy and it does raise a warning that Catalyst cannot find the uri_for for the action.


Solution

  • You should be able to introspect the currently dispatched action via

    my $action = $ctx->action;
    

    And since the action objects carry their attributes, you can check for a true value on one in your base chain call:

    sub root: Chained PathPart('') CaptureArgs(0) {
        my ($self, $ctx) = @_;
        $ctx->dispatch('/your/action/handling/this/error')
            if $ctx->action->attributes->{Disabled};
    }
    

    Then you can configure it in your config like described in http://search.cpan.org/dist/Catalyst-Runtime/lib/Catalyst/Controller.pm#action (using config::General syntax here):

    <controller Foo>
        <action "you_want_to_disable_this">
            Disable 1
        </action>
    </controller>