Mojolicous: configure plugin's actions



  • While writing plugins we can do something:

    $app->plugin( Auth => {
        user_check =>  sub{
            my( $user ) =  @_;
    
            # authentication_code_here;
        },
    );
    

    But I prefer to keep my sturtup sub clean. Thus I route to controller’s action:

    $app->plugin( Auth => { auth_check => 'auth#check' } );
    

    Doing so I need to check in my plugin passed value. Is it sub or shortcut auth#check?

    And to simplify checking I implement next route shourtcut

    sub register {
        my( $plugin, $app, $conf ) =  @_;
    
        $app->routes->add_shortcut( xto =>  \&xto );
        ...
        $app->routes->post  ( '/auth' )->xto( $conf->{ auth_check } )
            ->name( 'authenticate' );
    }
    
    sub xto {
        my $r =  shift;
        ref $_[0] eq 'CODE'?
            $r->to({ cb => $_[0] }):
            $r->to( @_ );
    
        return $r;
    }
    

    Unfortunately this is not supported by core ->to. I do not know why.

    Finally when we create route we want bind it to some action. The callback and controller’s method are both actions: 1

    my $cb = $field->{cb};
    $self->_callback($c, $cb, $last);
    ...
    _action($app, $c, $cb, $last);
    

    and 2

    $self->_controller($c, $field, $last);
    my $method = $field->{action};
    my $sub = $new->can($method);
    ...
    _action($app, $new, $sub, $last);
    

    Supporting this via ->to interface seems natural, does not?

    I discuss this on IRC. kraih was nor against, nor support that. Just not enough votes from other members.



    http://blogs.perl.org/users/eugen_konkov/2018/11/mojolicous-configure-plugins-actions.html

 

© Lightnetics 2018